! # / usr / bin / the env Python
#__author: HLC
#date: 2019/6/7
# listing formula
# DEF F (n-):
# n-return **. 3
# A = [F (X) for X in Range (10)] # foregoing parameters must later as
# Print (a) # [0,. 1,. 8, 27, 64, 125, 216, 343, 512, 729]
# generator
# a = (x for x in Range (10))
# Print (a)
# for I in a: # equal print (next (a)) traversal
# Print (I)
"" "
generator function and normal function a little different, is to return replaced yeild, wherein yeild is a syntactic sugar, the internal implementation iterator protocol, while maintaining the state may suspend
"" "
# DEF foo ():
# Print (" OK ")
the role played # yield 1 # yield fault,
Print # ( "OK2")
# yield 2
# for i in foo ():
# Print (i)
with the # for the back loop is iterables,Long as it has the __iter __ () method is iterables
DEF FIB # (max):
# n-, before, = 0,0,1 After
# n-the while <max:
# Print (After)
# before, After = After, before After +
# = n-n-+. 1
# FIB (. 8 )
# Builder difference in function: the function will all of the content is loaded into memory, the generator will only remove the desired value in time of need
. "" "
generator is an iterator;
iterators satisfy two conditions :
1, there iter method;
2, the next method;
for internal circulation three things:
1, call iterables iter method returns an iterator object
2, continue to call the iterator's next method object
3, the process StopIteration
"" "