Security Code: Indifferent Childe
Yield in python:
As we have mentioned in the previously published list of Python parsing and generator, the generator implements a similar effect to the list resolution, but we cannot do some operations that are part of the list resolution for the generator .
Because the generator itself is not a list, it simply simulates the behavior of a similar list, so many of the operations that are applied to the list are not valid for the Generator.
Because the builder expression does not directly create a list of sequential forms, it cannot be indexed, sliced, and cannot perform any general list Operations. For example: pop-up elements (pop ()), add elements (append ()), and so On. But we can convert the generator to a list by using the list Function.
In [1]: list ((i**2 for I in range (1,11))) out[1]: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In many cases we need to generate more complex results and do not want to implement them based on a list, but it is difficult to do so with a simple generator Expression. At this point we can fully implement a similar effect with a custom Function.
In [2]: def gennum (x): ...: y = 0 ...: while y <= x: ...: yield y ...: y += 1 ...: In [3]: g1 = Gennum (in [4]: type) (g1) out[4]: generatorin [5]: g1.next () Out[5]: 0In [6] : g1.next () out[6]: 1in [7]: g1.next () out[7]: 2in [8]: g1.next () Out[8]: 3in [9]: g1.next () out[9]: 4in [10]: g1.next () out[10]: 5in [11]: G1.next () out[11]: 6in [12]: g1.next () out[12]: 7in [13]: g1.next () Out[13]: 8in [14]: g1.next () out[14]: 9in [15]: g1.next () out[15]: 10in [16]: G1.next ()---------------------------------------------------------------------------stopiteration Traceback (most recent call last) <ipython-input-16-9066a8f18086 > in <module> ()----> 1 g1.next () stopiteration:
Yield itself is not a return value, but it can generate a generator object.
As you can see in the previous example, when we use yield in a function, we return a generator object.
Ask for the square of all positive integers within 1 to 20:
In [+]: def gennum (n): ...: count = 1 ...: while count <= n: ...: yield Count * * 2 ...: Count + = 1 ...: in []: g1 = Gennum (+) in [+]: for i in g1: ...: print i ...: 14916253649648110012114 4169196225256289324361400
Adorners in Python:
An adorner is a function decorator, and the adorner itself is a function whose main purpose is to enhance the functions of other functions.
Decorators are able to implement function code reuse, or function functions that are reused in different environments.
Adorners are a very well-known design pattern and are often used for such things as inserting logs, performance testing, transaction processing, and so On.
Adorners can be drawn out of a large number of functions and function-independent functions, the function itself as a core, if necessary, if the function of the core function is not enough, decorate the function of the call with the decorator, so the end of the next time when the need for other functions with the decorator to re-decorate it can be, This is the Decorator.
The adorner needs to accept a function object as its argument, and then wrap the function to enhance the Function.
Func with no parameters (the decorated function):
In [20]: def decorative (func): ...: def wrapper (): #定义一个包装器 ...: print "please say something: " ...: func () #调用func, This func is our own definition of ...: print "no zuo no die ... " ...: return wrapper ...:In [21]: @decorative #使用 @ symbol call Adorner ...: Def show (): #定义func, It doesn't matter what the name is, it just passes to the func parameter in the adorner ...: print "I ' m from mars." ...: show () :P lease Say something:i ' M from mars.no zuo no die ...
As shown in the example above, the show function itself has only one print statement, and after using the adorner, it becomes three print, where print can be changed to any other statement, which is the adorner of the Function.
Func with Parameters (decorated function):
In [+]: def decorative (func): ...: def wrapper (x): ...: print "please say something...>" ...: Func (x) ...: Print "no Zuo no die ..." ...: return wrapper ...: in []: @decorative ...: def s How (x): ...: print x ...: in []: show ("hello,mars.") Say something...>hello,mars.no Zuo no die ...
This article from "indifferent bo" blog, Please be sure to keep this source http://itchentao.blog.51cto.com/5168625/1885183
The yield and adorner of Python