For review: PEP 308 - If-then-else expression

Andrew Koenig ark at research.att.com
Sun Feb 9 11:19:05 EST 2003


Roy> Well, if we're going to take that argument, then we should also
Roy> be considering assignment as an expression so people can write
Roy> C-style loops such as "while x = foo():".  Otherwise, it's just
Roy> another special case, which makes it more complex for people to
Roy> understand how the language works.

I used to feel the same way.  Now, however, I realize that assignment
really is a special case, particularly in how it interacts with scopes.

For example:

        x = 42
        def f():
            x = 73
        f()
        print x

prints 42, not 73.  The syntactic position in which an assignment appears
affects the assignment's meaning.  As yet another example:

        x = 42
        def g():
            y = x
            x = 73
            return y
        print g()

The result is an exception:

    UnboundLocalError: local variable 'x' referenced before assignment

Here, the second line of g changes the meaning of the first line of g.

So I don't see how to avoid making '=' a special case without major
changes to the language.



-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list