Decorators, iterators, generators are all functional knowledge categories
1. Decorative Device
To understand the adorner, first understand the closure function.
closure function : 1, defines the function inside the function;
2. The function body code of the function contains a reference to the name of the outer scope (not the global scope);
3. The closure function is usually returned with return, which can then be called arbitrarily.
Eg: inner () is the closure function in the following code
def outer ():
X=1
def inner ():
Print (x)
return inner
F=outer ()
Adorner: that is, the application of a closure function.
What is an adorner? : A device that decorates its human being can be any callable object, or an adorner can be any callable object.
Adorner principle: 1, do not modify the source code of the Decorated object 2, do not modify the calling method of the decorated object
Object of the adorner: Add new features to the adorned object, following the 1 and 2 prerequisites
Import TimedefTimmer (func):definner (): Start=time.time () Res=func () Stop=time.time ()Print('run time is%s'% (stop-start)) returnResreturnInner@timmer#Index=timmer (Index)defindex (): Time.sleep (1) Print('Welecome to index') return1111Res=index ()Print(RES)2. iterators
Iterators : iterators are tools that iterate.
What is an iteration? : Refers to a repeating process, each repetition is called an iteration, and the result of each repetition is the initial value of the next repetition
Why do we have iterators? : For sequence type: Str,list,tuple, you can rely on the index to iterate over the value, but for Dict,set, the file, Python must provide us with an iterative way of not relying on the index-the iterator
What is an iterative object? : An iterative object refers to an object with a built-in __iter__ method, that is, obj.__iter__, as follows
' Hello '. __iter__
(__iter__).
[1,2,3].__iter__
{' A ': 1}.__iter__
{' A ', ' B '}.__iter__
Open (' A.txt '). __iter__
What is an iterator object? : An iterator object is the result of an iterative object execution obj.__iter__ (), whereas an Iterator object refers to an object that has a built-in __iter__ and built-in __next__ method.
The file type is an iterator object:
Open (' A.txt '). __iter__ ()
Open (' A.txt '). __next__ ()
Note: An iterator object must be an iterative object, and an iterative object is not necessarily an iterator object.
eg
b=='name':'Lili',' Age':' -','Sex':'male'}d_iter=iter (d)#D_iter=iter (d) whileTrue:Try: Print(Next (d_iter))exceptstopiteration: Break
Advantages:
Provides a unified, index-independent way to take a value
Iterators have only one value in memory at the same time, saving more memory
Disadvantages:
Can only be taken back, and it is disposable.
Cannot count the number of values, that is, the length
3. Generator
What is a generator? : As long as the yield keyword appears in the function body, then executing the function will not execute the function code, you will get a result, the result is the generator
Generators are iterators
deffunc ():Print('=====>1') yield1Print('=====>2') yield2Print('=====>3') yield3g=func ()#g.__iter__res1=Next (g)Print(res1) Res2=Next (g)Print(res2) Res3=Next (g)Print(RES3)
Yield Features:
1, yield provides us with a way to customize the iterator object
2. The difference between yield and return 1:yield can return multiple values #2: The state that the function is paused and resumed is saved by yield.
Python concept decorator, iterator, generator