nonlocal fails ?

Richard Damon richard.damon at gmail.com
Thu Nov 14 12:40:40 EST 2019


On Nov 14, 2019, at 12:18 PM, R.Wieser <address at not.available> wrote:
> 
> Rhodri,
> 
>> MyVar is a global here, so nonlocal explicitly doesn't pick it up.
> 
> I do not agree with you there (the variable being global).  If it where than 
> I would have been able to alter the variable inside the procedure without 
> having to resort to a "global" override (an override which is only valid for 
> the context its used in by the way, not anywhere else)
> 
> Than again, that is how it works in a few other languages, so I might have 
> been poisonned by them. :-)
> 
> Regards,
> Rudy Wieser

Assuming that one language works like another is a danger. It really pays to learn the base concepts of a language if you are going to be using it.

First, Python doesn’t really have ‘Variables’ like a lot of other languages (they don’t hold a bag of bytes), as Python names don’t hold values, but are just references to objects which actually hold the value (this can be important when several names all reference the same object, that means that if you mutate the object through one name they all see the change)

Also, for name lookup, the Python Compiler looks to see if the name is bound to an object (via assignment, etc) in the function, if so the name is by default local, (changeable by using a global or non local statement). If the name is never bound to something, then the name will be also looked for in the global namespace.

Yes, this is different than many other languages.


More information about the Python-list mailing list