8000 Teach the types module about generators. Thanks to James Althoff on the · python/cpython@3e7b1a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e7b1a0

Browse files
committed
Teach the types module about generators. Thanks to James Althoff on the
Iterators list for bringing it up!
1 parent ae1f65f commit 3e7b1a0

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Doc/lib/libtypes.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ \section{\module{types} ---
8383
An alternate name for \code{FunctionType}.
8484
\end{datadesc}
8585

86+
\begin{datadesc}{GeneratorType}
87+
The type of generator-iterator objects, produced by calling a
88+
generator function.
89+
\versionadded{2.2}
90+
\end{datadesc}
91+
8692
\begin{datadesc}{CodeType}
8793
The type for code objects such as returned by
8894
\function{compile()}\bifuncindex{compile}.

Lib/test/test_generators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,26 @@
367367
4-combs of [1, 2, 3, 4]:
368368
[1, 2, 3, 4]
369369
5-combs of [1, 2, 3, 4]:
370+
371+
# From the Iterators list, about the types of these things.
372+
373+
>>> def g():
374+
... yield 1
375+
...
376+
>>> type(g)
377+
<type 'function'>
378+
>>> i = g()
379+
>>> type(i)
380+
<type 'generator'>
381+
>>> dir(i)
382+
['next']
383+
>>> print i.next.__doc__
384+
next() -- get the next value, or raise StopIteration
385< BD1A /td>+
>>> iter(i) is i
386+
1
387+
>>> import types
388+
>>> isinstance(i, types.GeneratorType)
389+
1
370390
"""
371391

372392
# Fun tests (for sufficiently warped notions of "fun").

Lib/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def _f(): pass
3232
except:
3333
pass
3434

35+
def g():
36+
yield 1
37+
GeneratorType = type(g())
38+
del g
39+
3540
class _C:
3641
def _m(self): pass
3742
ClassType = type(_C)

0 commit comments

Comments
 (0)
0