As a python is an object-oriented programming language and iterators also considered an object that is used to Travers or iterates over the object which can hold some data and we need that data in looping order then iterators are the object which can do this job for python programmer.
Iterator object has methods like:
Iterators are mostly helping us to iterate the data containers like List, Dictionary, set.
The basic way of traversing in data objects is for loop and internally for loop also uses iterators so indirectly we use iterators only in the hidden way.
We can also create our own iterators on Python.
As we have discussed iterators in the previous topic that we can create the iterators in python to design iterators we need to implement two methods next and iter.
The creating iterators are considered more complex than comparing to using generators in python, yes we can use generators function instead of creating iterators.
To use the generator functions we have to understand two keywords in python.
It is used to return the final value from a python function
It is required when after returning one value you want to proceed function further for execution meaning return statement terminate function but yield continue after that also.
To create generators functions we can use yield and return statements.
def example_gen(): n = 1 print('first') yield n n += 1 print('second') yield n n += 1 print('end') yield n for item in example_gen(): print(item)
Decorators are the special feature of python that gives a facility to add the extra functionality to an existing function or a part of a code.
Decorators are used to implementing the met programming concept in python.
To understand the decorators it’s necessary that we should know almost everything in python basics like function as an object, parameter passing in python function and classes also.
Simple example decorators:
def some_function(parameter): """ here is the function body""" print("this is some_function " + parameter) some_function("callsing some function ") a = some_function # here we can pass a as a("calling a")
Python closure is the function that remembers the enclosing scopes inspite of absence in memory.
As we have discussed the decorators before so it’s easy to understand the closure in python. Because it is mention in decorators that to understand the decorators we need every basic of python including classes.
To understand the decorators we need to understand closures also.
So now coming to the point is what closure in python is.
Python provides the nesting of the functions, nesting of the functions is nothing but function inside functions.
def outer_function(msg): def inner_function() print(msg) inner_function()
Advantages of closure:
- We can avoid the use of global variable.
- Closure provides the mechanism of data hiding.
- When one method is required to implement in most of the cases the closure will be the best choice for python programmers.
- When we use decorators then closures provides a lot more features to achieve the task with decorators.
Click Here-> Get Python Training with Real-time Projects//php comment_form(); ?>