How to modify local variables from internal functions?
apt.shansen at gmail.com
Sat Oct 24 02:31:00 CEST 2009
On Fri, Oct 23, 2009 at 5:19 PM, kj <no.email at please.post> wrote:
> I like Python a lot, and in fact I'm doing most of my scripting in
> Python these days, but one thing that I absolutely *****DETEST*****
> about Python is that it does allow an internal function to modify
> variables in the enclosing local scope. This willful hobbling of
> internal functions seems to me so perverse and unnecessary that it
> delayed my adoption of Python by about a decade. Just thinking
> about it brings me to the brink of blowing a gasket... I must go
> for a walk...
Is there some other trick to modify local variables from within
> internal functions?
In Python 3, there's the "nonlocal" keyword to let you do just this.
Otherwise, no. The only way to do what you want is to use some mutable
object to 'hold' your data in the enclosing namespace, and modify that
object. You can use a list like you did, or some simple "class namespace:
pass" with "ns = namespace()" and "ns.jobs = 1" or whatever you prefer.
But otherwise, you just can't do that. Gotta deal.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list