1 Generator, Python builder,2 3 first, familiarize yourself with the definition of the child,4 generator Function Generator functions ,5A generator function is one that is stored in the definition body'yield'the function of the keyword.6 when the generator function is called, the function returns a generator.7 8A function that has theyieldKeywordinch9 Its body. When invoked, a generator func‐Ten tion returns a generator. One A Generator Generator, - the essence of the generator generator is the iterator iterator, built by the generator function or generator expression, -Not by iterating over the set collection'Production'the value in the collection. the typical usage Fibonacci sequences. Because the Fibonacci series is infinite, it cannot be placed into a collection. - The term generator is sometimes used to describe a generator function. - - An iterator built with a generator function + ora generator expression that may produce - values without necessarily iterating over a +Collection The canonical example isa gen‐ A Erator to produce the Fibonacci series atwhich, because it isInfinite, would never fit - inchA collection. The term issometimes used - to describe a generator function, besides the -Object that results fromcalling it. - - Generator Expression Builder expressions, in generator expression is enclosed in parentheses, and the syntax is the same as the list derivation lists comprehensive, - But the return is a generator, not a list. to Generator Expressions Generator expression can be viewed as a lazy evaluation of the lazy version of the list comprehensive. + -An expression enclosedinchparenthesis us‐ the ing the same syntax of a list comprehen‐ * Sion, but returning a generator instead of a $ list. A Generator expression can be under‐Panax Notoginseng stood as a lazy version of a list comprehen‐ - Sion. See lazy. the + Lazy laziness evaluation, ALazy evaluation of lazy refers to a'on Demand'an iterative object for the output element iterable the An iterable object which produces items on + demand. In Python, generators is lazy. - contrast eager. $ $ - - Example Fibonacci series, the deffib (): -A, b = 0, 1Wuyi while1: the yieldB#The generator function defines the yield keyword in the body -A, B = B, A +b Wuf =fib () - Print(f) AboutCOF = 8 $ whileCOF: - Print(F.__next__())#The generator is an iterative generator is iterable and generator iterators -COF-= 1 - A output, +<generator Object fib at 0x02e692d0>#generator function ' build ' generator the1 -1 $2 the3 the5 the8 the13 -21st in the Generator Object, the Python implements generator iterators through the Builder object. AboutGenerator is usually created by the generator function (defines theyieldkeyword function), the instead of directly by calling Pygen_new () or Pygen_newwithqualname (). the the pygenobject, + Generator Object C struct. - the typedef struct {Bayi/* The gi_ prefix isIntended to remind of Generator-iterator. */ the _pygenobject_head (GI) the } pygenobject; - - #define _pygenobject_head (prefix) \ the Pyobject_head the/* Note:gi_frame can be NULLifThe generator is "finished"*/ thestruct _frame *prefix##_frame; \ the/* TrueifGenerator isBeing executed. */ -Char prefix##_running; \ the/* The Code object backing the generator */ thePyobject *prefix##_code; \ the/* List of weak reference. */ 94Pyobject *prefix##_weakreflist; \ the/* Name of the generator. */ thePyobject *prefix##_name; \ the/* Qualified name of the generator. */ 98Pyobject *prefix##_qualname; About - Pytypeobject Pygen_type101 Generator object's Python type objects102 103 int Pygen_check (pyobject *ob) 104 if the OB is a Generator object Generator objects, returns True, the Note, OB cannot be a Null object.106 107 int pygen_checkexact (pyobject *ob) 108 if the type of OB is Pygen_type, returns True,109 Note, OB cannot be a Null object. the 111 pyobject* pygen_new (pyframeobject *frame) the Return value:new Reference.113Creates and returns a new Pyframeobject-based *frame's Generator object. theNote, *frame is not a Null object. the the pyobject* pygen_newwithqualname (pyframeobject *frame, Pyobject *name, Pyobject *qualname) 117 Return value:new Reference.118Creates and returns a new Pyframeobject *frame-based generator object,__name__property is *name,119 __qualname__property is *Qualname. -Note, *frame is not a Null object. 121 122 References,123 124https://docs.python.org/3/c-api/gen.html?highlight=Generator thehttps://www.python.org/dev/peps/pep-0255/
Generator-python Generator