Note python 13-day13

1, iterators

As long as it can be for circulation of data types will certainly have __iter__ method

Print ( ' __iter__ '  in the dir (BOOL)) # determines whether or not containing __iter__

The return value is an iterator after a list of executed __iter __ ()

= L [l, 2,3 ] 
Iterator = L. the __iter__ () # returns an iterator 
Print (Iterator. __next__ ()) # call list of elements by iterator 
Print (Iterator. __next__ ()) 

# the Iterable be iterative -> __iter__ # __iter__ long as it contains iterative methods are available 
# [] .__ ITER __ () iterator -> __next__ # next to it by a value of one from the iterator

Iterator concept
iterator protocol - internal and __iter__ method comprising __next__ iterators

Iterator protocol and protocol iteration
may be all be iterative loop for
internal iteration may have __iter__ method
as long as the iterator must iteration can
be iterative .__ iter __ () method can be used to obtain an iterator
iterator in the __next __ () method may obtain a value of a

2, generator

Yield keyword long as it contains a generator function is a function of
yield and need not be shared and written in the return function

DEF Generator ():
     Print (. 1 )
     the yield  ' A ' 
# generator function: after a generator will be performed as the return value 
RET = Generator ()
 Print (RET)
 Print (RET. __next__ ())

3, monitor file input Lieh (but not implemented, like the video but no code)

def tail(filename):
    f = open(filename,encoding='utf-8')
    while True:
        line = f.readline()
        if line.strip():
            yield line.strip()

g = tail('001')
for i in g:
    print(i)

 

Guess you like

Origin www.cnblogs.com/xiao-le/p/11498769.html