[Python-ideas] Globalize lonely augmented assignment

Demur Rumed junkmute at hotmail.com
Sat Jun 12 03:18:48 CEST 2010

> However, I believe this is a case where the cognitive cost of changing 
> the status quo isn't worthwhile. New Python users can be taught very 
> quickly that assigning to a variable from a different scope requires a 
> global or nonlocal declaration.
> While existing users could be taught easily enough that that was no 
> longer necessary for augmented assignment, how much real world code 
> would actually benefit? (now, Paul Graham's accumulator hobby horse 
> doesn't count as real world code)

I believe it would be simpler to learn that variables are _only_ local if bound with the assignment operator. I view the augmented assignment operators as different beasts. This patch doesn't quite meet its goals in that respect. I'd like to further the locality of a variable to "A variable is local if, and only if, it is first referenced as the left hand side of an assignment on all code paths." This patch fails to set that rule

For example,

def f(x):
    if len(a)>5:a=[5]

If a is bound as a local, this throws an UnboundLocalError. Why then is it not set to try the global namespace, that place we cannot be so certain of the exception in?

It comes down to the view of UnboundLocalError as a feature or a bug
Occupez vos temps morts avec les jeux Messenger!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100611/7728193c/attachment.html>

More information about the Python-ideas mailing list