[Tutor] "farkadoodle" or: unique global names, was Re: Data persistence problem

eryksun eryksun at gmail.com
Sun Jun 23 00:13:50 CEST 2013


On Sat, Jun 22, 2013 at 3:59 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
>
> I was playing around with this a bit and arrived at the following
> surprising (for me at least) result. I thought the global/local/nonlocal
> keywords could be used to get values from another scope. Though this
> could also happen implicitly, e.g. if only x = "I am global" is defined
> and x is used (and not redefined) inside a function, then python still
> knows this variable inside that function.
>
> Is there any way to make this work? (it may not be desirable, though)

local isn't a keyword. Maybe you're thinking of the locals() function.
The global and nonlocal keywords define the scope of a name for the
entire block. You can't flip it on and off like a toggle switch. That
would give me headaches. Anyway, try to avoid globals and use
descriptive names. If you still have a clash, use globals()[name].


More information about the Tutor mailing list