[Python-Dev] 'fast locals' in Python 2.5

Armin Rigo arigo at tunes.org
Wed Jun 7 10:39:41 CEST 2006


Hi,

On Wed, Jun 07, 2006 at 02:07:48AM +0200, Thomas Wouters wrote:
> I just submitted http://python.org/sf/1501934 and assigned it to Neal so it
> doesn't get forgotten before 2.5 goes out ;) It seems Python 2.5 compiles
> the following code incorrectly:

No, no, it's an underground move by Jeremy to allow assignment to
variables of enclosing scopes:

    [in 2.5]
    def f():
        x = 5
        def g():
            x += 1
        g()
        print x     # 6

The next move is to say that this is an expected feature of the
augmented assignment operators, from which it follows naturally that we
need a pseudo-augmented assignment that doesn't actually read the old
value:

    [in 2.6]
    def f():
        x = 5
        def g():
            x := 6
        g()
        print x    # 6

Credits to Samuele's evil side for the ideas.  His non-evil side doesn't
agree, and neither does mine, of course :-)

More seriously, a function with a variable that is only written to as
the target of augmented assignments cannot possibly be something else
than a newcomer's mistake: the augmented assignments will always raise
UnboundLocalError.  Maybe this should be a SyntaxWarning?


A bientot,

Armin


More information about the Python-Dev mailing list