PEP 255: Simple Generators, Revised Posting

Bernhard Herzog bh at intevation.de
Mon Jun 25 05:52:58 EDT 2001


"Tim Peters" <tim.one at home.com> writes:

> [Bernhard Herzog]
> > I wasn't aware of that! For that matter, I wasn't even aware that the
> > compiler removed "if 0" blocks. When was that introduced. It even
> > "works" with 1.5.2.
> 
> It was introduced along with "assert":  under the covers, assert gets
> changed into
> 
>     if __debug__:
>         do the assert code
> 
> __debug__ is a compile-time constant, and it was thought important that
> asserts leave no trace (not even bytecode) under -O.  The mechanism for
> getting rid of
> 
>     if __debug__:
> 
> at compile-time just happens to recognize literal numeric zeroes and empty
> strings as meaning "false" too.

Thanks. I knew about __debug__ and assert but I thought this
optimization would only happen with the -O option. If I understand this
correctly the optimization is always done actually, regardless of
optimization options and the only reason __debug__ code is not removed
when no optimization option is given is that __debug__ is true in that
case.

> > Being able to easily write a function that does nothing (in fact it's
> > the most trivial function one can write) is very useful in practice, so
> > as a generalization being able to write empty generators might be
> > useful, too. I'm not sure about that, though, because I don't have
> > enough experience with them.
> 
> Can't say I see much use for it.  If I did, I'd probably write what
> everybody hates:
> 
> def emptyg():
>     return
>     yield None  # never reached
> 
> Stick that in a utility module and you'll never have to think about it
> again.

Only until the compiler becomes smart enough to remove that unreachable
yield statement. :)

  Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



More information about the Python-list mailing list