Python Generators: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
* [[Python_Language#Generators|Python Language]]
* [[Python_Language#Generators|Python Language]]
* <tt>[[Python_Language_Functions#reversed|reversed()]]</tt>
* <tt>[[Python_Language_Functions#reversed|reversed()]]</tt>
* [[Python_Iterators#Overview|Python Iterators]]


=TODO=
=TODO=
Line 11: Line 12:


=Overview=
=Overview=
A generator is a way to construct a new [[Python_Iterators#Overview|iterable]] object. Whereas normal functions execute and return a single result at a time, generators can return a sequence of multiple values by pausing and resuming execution each time the generator is used. To create a generator, use the <code>yield</code> keyword instead of <code>return</code> in a function.
A generator is a way to construct a new [[Python_Iterators#Overview|iterable]] object. Whereas normal functions execute and return a single result at a time, generators can return a sequence of multiple values by pausing and resuming execution each time the generator is used. To create a generator, use the <code>[[Python_Language#yield|yield]]</code> keyword instead of <code>return</code> in a function.
<syntaxhighlight lang='py'>
def squares(n=10):
  for i in range(1, n+1):
    yield i ** 2
</syntaxhighlight>
When the generator is called no code is immediately executed:
<syntaxhighlight lang='py'>
gen = squares()
</syntaxhighlight>
It is not until you request elements from the generator that it begins executing code:
<syntaxhighlight lang='py'>
for x in gen:
  print(x)
</syntaxhighlight>
=Generator Comprehensions=
A generator expression is the generator analogues to [[Python_Comprehensions#List_Comprehensions|list]], [[Python_Comprehensions#Dictionary_Comprehensions|dictionary]] and [[Python_Comprehensions#Set_Comprehensions|set]] comprehensions. To create one, enclose what would be otherwise a list comprehension within '''parentheses''' instead of brackets. Depending on the number of elements produced by the comprehension expression, the generator expression can sometimes be meaningfully faster.
<font color=darkkhaki>
* PROCESS [[IPy]] Comprehensions Page 88.
</font>


=Use Cases=
=Use Cases=


Create a [[Python_Language_List#Pass_a_Generator_Expression|list with a generator]].
Create a [[Python_Language_List#Pass_a_Generator_Expression|list with a generator]].

Latest revision as of 19:11, 17 May 2024

Internal

TODO

  • PROCESS IPy Generators Page 101.
  • PROCESS PyOOP "Generator expressions"
  • PROCESS PyOOP "Generators" + "Yield items from another iterable"

Overview

A generator is a way to construct a new iterable object. Whereas normal functions execute and return a single result at a time, generators can return a sequence of multiple values by pausing and resuming execution each time the generator is used. To create a generator, use the yield keyword instead of return in a function.

def squares(n=10):
  for i in range(1, n+1):
    yield i ** 2

When the generator is called no code is immediately executed:

gen = squares()

It is not until you request elements from the generator that it begins executing code:

for x in gen:
  print(x)

Generator Comprehensions

A generator expression is the generator analogues to list, dictionary and set comprehensions. To create one, enclose what would be otherwise a list comprehension within parentheses instead of brackets. Depending on the number of elements produced by the comprehension expression, the generator expression can sometimes be meaningfully faster.

  • PROCESS IPy Comprehensions Page 88.

Use Cases

Create a list with a generator.