PEP 255: Simple Generators, Revised Posting

Jeremy Hylton jeremy at alum.mit.edu
Sat Jun 23 16:29:18 EDT 2001


> That didn't yield "the expected" UnboundLocalError for the same reason:
the
> affected blocks are actually removed by the *parser*, and "the compiler"
proper
> never knows they were there -- the "if 0" blocks aren't even in the parse
tree.

Actually, they are in the parse tree.

The compiler ignores "if 0" blocks by explicitly calling is_constant_false()
in com_if_stmt and in the if_stmt case of symtable_node().  So it's easy
enough to examine that part of the tree.

> That makes it difficult to repair, and since nobody writes "if 0" in real
life,
> that in turn makes the cost/benefit ratio very high.  If you disagree,
submit a
> patch <wink>.

This would be easy to fix in the case of yield for generators, except that
I'm not sure it's easy to fix for names in general.  There are lots of other
strange possibilities:

def f():
    if 0:
        exec foo
    def g(y):
        return x + y

Should this get a SyntaxError about the bare exec making resolution of free
variable x ambiguous?

def g(x):
    if 0:
        global x

Should this get a SyntaxError about an argument being declared global?

I'm inclined not to worry about them, because they'd be a PITA to deal with
and the "right" choice is far from obvious -- unless Guido wants to offer
some pronouncements on just what the heck they mean.

Jeremy





More information about the Python-list mailing list