python basis of a review day14

Iterators and generators 
iterator:
Double Down: Method rarely called directly. In general, triggered by other syntaxes
may be iterative - protocol may contain an iterative method of __iter__ ( '__iter__' in dir (data))
can be for a certain iteration loop
iterator protocol: containing __iter__ the method and __next__
iterators must be iterative, may be iterative approach can be obtained by calling an iterator ITER ()
iterator characteristics:
easy to use, and can take all the data fetched once
saving memory space

Builder
Builder the essence is the iterator
generator forms
generator function
generator expression
generator function:
function containing the yield keyword is a generator function of
characteristics:
after calling the function does not perform the function returns a generator
each time the next method invocation when a value to be taken
until the end of the last taken, in the next execution will be given

sketch generator implemented: a file, segment file read from
the readline
read (10)
in front of the read out contents together with a '* ** ', and then return to the caller
DEF Generator ():
for I in Range (20 is):
the yield' wow ha% s'% i

g = generator () # call to obtain a generator function generator
Print (List (G))
RET = G .__ Next __ () # g .__ next__ each execution is the value from the generator, the generator indicates functions the code continues execution
Print (RET)
NUM = 0
for I in G:
NUM = +. 1
IF NUM> 50:
BREAK
Print (I)

several methods values from builder
Next
for
cast data types: memory for

Reproduced in: https: //www.cnblogs.com/wang-tan/p/11062954.html

Guess you like

Origin blog.csdn.net/weixin_34239169/article/details/93657162