1 #-*-coding:utf-8-*-2 #LC3 #List-generated4 deffunc (x):5 Print(x)6 return*x7 Print([Func (i) forIinchRange (10) ])8 9A = [i*2 forIinchRange (1000000000000)]#This list generation takes up a lot of memory space, and the list already exists in memory without a callTen Len (a) One #Generator Ab = (i*2 forIinchRange (1000000000000))#generator, only in the time of call B, will be calculated according to the rules, the results are returned to B, when used to occupy memory, fast - #The generator does not support slicing -B[100]#This can't be taken directly. the forIinchB: - Print. IO - - #Generator + #1. The corresponding data is generated only when it is called - #2. Record the current location only + #3. Only one __next () __ Method A at #Fibnacci function - #The function generator, using yield, uses the yield function, which is not a function, but a generator - deffib (max): -N,a,b = 0,0,1 - while(n<max): -n + = 1 in yieldB#yield returns the current value to the function, executes once, and the __next__ method calls the Yeild value -A, B = b,a+b to return "---done---" + fromCollectionsImportIterator - thef = fib (1) * Print("-------", Isinstance (f,iterator)) $ whileTrue:Panax Notoginseng Try: -g = f.__next__() the Print("function Generator:", G) + exceptstopiteration as E: A Print("Generator is Stop,value is", E.value) the Break + #send wakes the current generator and passes a value to Yeild - $ defConsumer (name): $ Print("%s ready to start eating buns."%name) - whileTrue: -Baozi =yield the Print("Bun {%s} has come, {%s} please eat it!"%(baozi,name)) - Wuyi defproducer (name): theC1 = Consumer ("A")#This step only turns the function into a generator, and the generator does not execute, and if you want to do that you need to call the __next__ method, the __next__ method will break if it encounters yield -C2 = Consumer ("B") WuNext (C1)#equals c1.__next__ () method - Next (C2) About Print("%s made buns."%name) $ forIinchRange (6): - Print("Bun [%s] Okay, split into two parts."%i) - c1.send (i) - c2.send (i) A +Producer"Lvcheng") the - #1. The execution flow of the function is executed in order, the function ends when it encounters the return and the last line, and the generator returns when it encounters the yield statement each time the __next__ () is called, and at the time of the next call, Continue at the last yield execution location $ #2. Objects that can be iterated through a for loop can be isinstance () to determine whether an object is an iterative object, a list, a tuple, a dictionary, a string is an iterative object, and an integer is not the fromCollectionsImportIterable#import Iterable to determine whether an object is an iterative object the Print(Isinstance ([],iterable))#True the Print(Isinstance ({},iterable))#True the Print(Isinstance ((), iterable))#True - Print(Isinstance ("ABCD", iterable))#True in Print(Isinstance (x forXinchRange (Ten)), iterable))#True the Print(Isinstance (100,iterable))#False the About #iterators the #1. An object that can be called by the next () function and continually returns the next value is called an iterator the #2. You can judge whether an object is iterator with Isinstance () the #3. Generators are iterators, but lists, strings, dictionaries, etc. are not iterators, you can use the ITER () function to become iterators + fromCollectionsImportIterator - Print(Isinstance (x forXinchRange (Ten)), Iterator))#True the Print(Isinstance ([],iterator))#FalseBayi Print(Isinstance (ITER ([]), Iterator))#True
Python---builder, iterator