Generator methods are defined by the use of the **yield keyword:**

=20

def fibonacci(): a, b =3D 0, 1 while true: yield b a, b =3D b, a+b=20

Given the definition above the following program would print the first f= ive elements of the Fibonacci series:

=20=20

for index as int, element in zip(range(5), fibonacci()): print "$(index+1): $element"=20

So although the generator definition itself is unbounded (a while true l= oop) only the necessary elements will be computed, five in this particular = case as the zip builtin will stop asking for more when the range is exauste= d.

=20Generator methods are also a great way of encapsulating iteration logic:=

=20=20

def selectElements(element as XmlElement, tagName as string): for node as XmlNode in element.ChildNodes: if node isa XmlElement and tagName =3D=3D node.Name: yield node=20