First, the decoration device
Adorners make it possible to perform other additional functions before and after a function is executed, a way to dynamically add functionality during the run of the code, called an "adorner" (Decorator), and the adorner functions very strongly. Adorners generally accept a function object as a parameter to enhance it plainly: equivalent to a constructor in C + + and a destructor
- The adorner itself is a function used to decorate other functions
- An adorner is a closure function that is a nested function that provides an environment for nesting functions through an outer function
- Adorner in permission control, add extra functions such as log, send mail with more
1, the original function without parameters of the adorner
Suppose: I define a function Lyshark, and now want to print a paragraph before the function runs without changing the original function definition, and then print another paragraph after the function is run, how do you implement such a function? Look at the following implementation methods:
>>>ImportOS>>>ImportSYS>>> >>>defouter (function):definner ():Print("before the main function executes, I should be executed first! ") Result=function ()Print("after the main function executes, do me! ") returnresultreturnInner
# (1) @ + function name directly on the functions that need to be decorated a line # (2) automatically executes the outer function and passes the following function name Lyshark () as a parameter to outer () # (3) inner The return value of the outer function, and re-assigns to the Lyshark () function
>>> @outer def Lyshark (): print ( " lyshark The main function body, the adorner is decorating me (* ^_^ *) " ) return " >>> ret=lyshark () Before the main function executes, I should be executed first! The main function body of the Lyshark, the adorner is decorating me ( * ^_^ * "after the main function executes, to execute me! >>> >>> print ( ,ret) The return value of the Lyshark () function: Lyshark returned the >>>
2, the original function with a parameter of the adorner
Suppose: We pass a parameter to the function on the previous basis to see how it reacts, to stimulate it
>>>ImportOS>>>ImportSYS>>> >>>defouter (function):definner (args):Print("before the main function executes, I should be executed first! ") ret=function (args)Print("after the main function executes, do me! ") returnretreturnInner>>>@outerdefLyshark (args):Print(args)return "Lyshark returned.">>> >>> Ret=lyshark ("Hello world!"before the main function executes, I should be executed first! Hello world! after the main function executes, to execute me! >>> >>>Print("The return value of the Lyshark is:", ret) The return value of the Lyshark () function is: Lyshark returns the>>> >>>
3, the original function with two parameters of the adorner
Hypothesis: We pass two parameters to the function on the previous basis to see how it reacts and stimulates it
>>>ImportOS>>>ImportSYS>>> >>> >>>defouter (function):definner (x, y):Print("before the main function executes, I should be executed first! ") ret=function (x, y)Print("after the main function executes, do me! ") returnretreturnInner>>>@outerdefLyshark (x, y):Print(x, y)return "Lyshark returned.">>> >>> Ret=lyshark ("Hello","Lyshark"before the main function executes, I should be executed first! Hello Lyshark after the main function executes, to execute me! >>> >>>Print("The return value of the Lyshark () function is:", ret) The return value of the Lyshark () function is: Lyshark returns the>>> >>>
4, pass a universal parameter try
>>>ImportOS>>>ImportSYS>>> >>>defouter (function):defInner (*args,**Kwargs):Print("before the main function executes, I should be executed first! ") ret=function (*args,**Kwargs)Print("after the main function executes, do me! ") returnretreturnInner>>> >>>@outerdefLyshark (*args):Print(args)return "Lyshark returned.">>> >>> num=[1,2,3,4,5]>>> ret=before the Lyshark (NUM) Main function executes, I should be executed first! ([1, 2, 3, 4, 5],) after the main function executes, to execute me! >>> >>>Print("The return value of the Lyshark () function is:", ret) The return value of the Lyshark () function is: Lyshark returns the>>> >>>
Second, generator
- Unlike a normal function, a generator is a function that returns an iterator that can be used only for iterative operations, and simpler to understand that the generator is an iterator.
- In Python, a function that uses yield is called a generator (generator).
- In the process of calling the generator to run, the function pauses and saves all current run information every time the yield is encountered, returns the value of yield, and continues running from its current location the next time the next () method is executed.
- Invokes a generator function that returns an Iterator object.
Three, iterators
Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration. In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it ideal for traversing large or infinite collections, such as several G files
- Iteration is one of the most powerful features of Python and is a way to access the elements of a collection.
- The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without backing back.
- An iterator is an object that remembers where to traverse.
- Iterators have two basic methods:iter () and next ().
- A string, list, or tuple object can be used to create iterators.
Reference: http://www.runoob.com/
The Python devops development Decorator & Builder & Iterator (v)