PEP 255: Simple Generators, Revised Posting
Bernhard Herzog
bh at intevation.de
Sat Jun 23 14:01:41 EDT 2001
"Tim Peters" <tim.one at home.com> writes:
> The yield statement may only be used inside functions. A function that
> - contains a yield statement is called a generator function.
> + contains a yield statement is called a generator function. A generator
> ? +++++++++++++
> + function is an ordinary function object in all respects, but has the
> + new CO_GENERATOR flag set in the code object's co_flags member.
With the current implementation that doesn't seem to be entirely true
(CVS from somtime 2001-06-23 afternoon UTC):
>>> def empty():
... if 0: yield 0
...
>>> for i in empty():
... print i
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iter() of non-sequence
>>> import dis
>>> dis.dis(empty)
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_CONST 0 (None)
9 RETURN_VALUE
>>> empty.func_code.co_flags
3
It seems that the mere lexical presence of the yield statement doesn't
make a function a generator. The compiler apparently optimized the if
statement away before testing whether it's a generator.
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://mapit.de/
More information about the Python-list
mailing list