"The ``yield`` statement
***********************
yield_stmt ::= yield_expression
The ``yield`` statement is only used when defining a generator function, and is only used in the body of the generator function. Using a ``yield`` statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.
When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator. The body of the generator function is executed by calling the generator's ``next()`` method repeatedly until it raises an exception.
When a ``yield`` statement is executed, the state of the generator is frozen and the value of **expression_list** is returned to ``next()``'s caller. By "frozen" we mean that all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time ``next()`` is invoked, the function can proceed exactly as if the ``yield`` statement were just another external call.
As of Python version 2.5, the ``yield`` statement is now allowed in the ``try`` clause of a ``try`` ... ``finally`` construct. If the generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), the generator-iterator's ``close()`` method will be called, allowing any pending ``finally`` clauses to execute.
Note: In Python 2.2, the ``yield`` statement was only allowed when the ``generators`` feature has been enabled. This ``__future__`` import statement was used to enable the feature:
from __future__ import generators"
------
If you run this in python 3 then you have to use parentheses for the print statement as print has become a function, print("prints this and the variable", variable )
It is written in Python2.6
Yield example:
Just a list to be used as a test subject :)
01aList=["good","this","is","a","good","list"]
This is a generator function because it is using yield.
yield generates an answer for every element in the list in this example.
It is looking for the word good in aList
02def myYield(theList):
03 for x in theList:
04 if x=="good":
05 yield "Yes it is =)"
06 else:
07 yield "No it's not =("
03 for x in theList:
04 if x=="good":
05 yield "Yes it is =)"
06 else:
07 yield "No it's not =("
This is a ordinary function but it has two return statements witch can be a bit misleading.
It is looking for the word good in aList
08def myReturn(theList):
09 for x in theList:
10 if x=="good":
11 return "Yes it is =)"
12 else:
13 return "No it's not =("
09 for x in theList:
10 if x=="good":
11 return "Yes it is =)"
12 else:
13 return "No it's not =("
As long as yield generates anything it will be printed out in the for loop.
14print "This is generated from the myYied function"
15for element in myYield(aList):
16 print "Is it good? ", element
17print ""
15for element in myYield(aList):
16 print "Is it good? ", element
17print ""
If you try to print myYield(aList) it just prints out where in the memory the generator object myYield is.
18print "This happens when you try to print a generator function:"
19print myYield(aList)
20print ""
19print myYield(aList)
20print ""
It just handles the first element in the list and iterates over the returned answer character by character.
21print "This is generated from the myReturn function:"
22for element in myReturn(aList):
23 print "Is it good? ", element
24print ""
22for element in myReturn(aList):
23 print "Is it good? ", element
24print ""
if you print the returned answer from the function myReturn(aList) it will still just check the first element in the list and print out the answer for that element.
25print "This happens if you try to print the myReturn function:"
26print myReturn(aList)
26print myReturn(aList)
Output:
This is generated from the myYied function
Is it good? Yes it is =)
Is it good? No it's not =(
Is it good? No it's not =(
Is it good? No it's not =(
Is it good? Yes it is =)
Is it good? No it's not =(
This happens when you try to print a generator function:
generator object myYield at 0xb7759234
This is generated from the myReturn function:
Is it good? Y
Is it good? e
Is it good? s
Is it good?
Is it good? i
Is it good? t
Is it good?
Is it good? i
Is it good? s
Is it good?
Is it good? =
Is it good? )
This happens if you try to print the myReturn function:
Yes it is =)
As you can see the myYield function did as expected when you used the for loop to iterate over it. But the myReturn function just iterated over the characters in the sentence "no it's not". If the first word in aList had been good then it would have iterated over "yes it is" instead.
I hope this helped you to see the difference between yield and Return.
If you have any good examples where yield is in good use or if you just want to comment my code please do.
Inga kommentarer:
Skicka en kommentar