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

Tim Peters tim_one at
Fri Aug 20 03:30:11 EDT 1999

[posted & mailed]

[Timothy R Evans]
> :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.

[Tom Christiansen]
> Perhaps it could give a better error message, pretty please?

I whined about this for years myself, and a month or two ago finally wrote a
patch to make it raise UnboundLocalError instead -- which is what you'll get
if you use the current CVS version of Python.  It should be made clear that
Timothy's wibble example fails on the "print x" line, though, not the

> Sometimes driving Python feels like driving Ken Thompson's car.
> It just keeps saying> "?" and you're expected to know what
> that means.  :-(

But also like a sleek modern car with idiot lights, Python's syntax is so
simple you soon figure out that "?" means what it says <wink>.

change-the-oil-or-fix-the-syntax-ly y'rs  - tim

More information about the Python-list mailing list