Compound Assignment Operators ( +=, *=, etc...)

Timothy R Evans tre17 at pc142.cosc.canterbury.ac.nz
Thu Aug 19 20:46:45 EDT 1999


Tom Christiansen <tchrist at mox.perl.com> writes:

>      [courtesy cc of this posting mailed to cited author]
> 
> In comp.lang.python, 
>     Timothy R Evans <tre17 at pc142.cosc.canterbury.ac.nz> writes:
[snip]
>     def foo():
> 	x = x + 2
> 	return x
> 
> That's not legal even now.  You need the global.
> 
[snip]

Oops, you're right of course, you can access globals but you can't
override a global definition if you have already used the global
value.  Quite strange behaviour really.

x = 5
def foo():
    print x # works, prints 5
def bar():
    x = 6 # works, but doesn't change the global
def wibble():
    print x
    x = 6 # this fails with NameError

Seems like a good behaviour as it stops you doing dumb things.

--
Tim Evans





More information about the Python-list mailing list