Teach the types module about generators. Thanks to James Althoff on the

Iterators list for bringing it up!
This commit is contained in:
Tim Peters 2001-06-25 19:46:25 +00:00
parent ae1f65ff82
commit 3e7b1a04a0
3 changed files with 31 additions and 0 deletions

View File

@ -83,6 +83,12 @@ The type of user-defined functions and lambdas.
An alternate name for \code{FunctionType}.
\end{datadesc}
\begin{datadesc}{GeneratorType}
The type of generator-iterator objects, produced by calling a
generator function.
\versionadded{2.2}
\end{datadesc}
\begin{datadesc}{CodeType}
The type for code objects such as returned by
\function{compile()}\bifuncindex{compile}.

View File

@ -367,6 +367,26 @@ Next one was posted to c.l.py.
4-combs of [1, 2, 3, 4]:
[1, 2, 3, 4]
5-combs of [1, 2, 3, 4]:
# From the Iterators list, about the types of these things.
>>> def g():
... yield 1
...
>>> type(g)
<type 'function'>
>>> i = g()
>>> type(i)
<type 'generator'>
>>> dir(i)
['next']
>>> print i.next.__doc__
next() -- get the next value, or raise StopIteration
>>> iter(i) is i
1
>>> import types
>>> isinstance(i, types.GeneratorType)
1
"""
# Fun tests (for sufficiently warped notions of "fun").

View File

@ -32,6 +32,11 @@ try:
except:
pass
def g():
yield 1
GeneratorType = type(g())
del g
class _C:
def _m(self): pass
ClassType = type(_C)