]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Teach the types module about generators. Thanks to James Althoff on the
authorTim Peters <tim.peters@gmail.com>
Mon, 25 Jun 2001 19:46:25 +0000 (19:46 +0000)
committerTim Peters <tim.peters@gmail.com>
Mon, 25 Jun 2001 19:46:25 +0000 (19:46 +0000)
Iterators list for bringing it up!

Doc/lib/libtypes.tex
Lib/test/test_generators.py
Lib/types.py

index 8ade4a6be956aa18e0fe0a49e9e2b22fbde2cb7e..35495eba6917637611dc4b5a045a8c2547cc422f 100644 (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}.
index e0d6cfab1db99f24a904cd8fe92ea97b0700c38b..884cbc0ff8dbf983dca71200b588407976407a3d 100644 (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").
index a71a4db4e9cabda0b7af0f9857a876ff61ee35ae..85962bada48eb8e584b325a5c03991ba27b11e68 100644 (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)