Scoped change of a global variable
Steven Majewski
sdm7g at Virginia.EDU
Wed Jan 2 18:29:54 EST 2002
An interesting problem: that sort of construct, both in builtin's like
'with-open-file' and custom ones using 'let' is one of the most commonly
used (and useful) applications of nested scope in Lisp
Now that Python 2.2 has nested scopes, I would have thought there was a
simple way to do it, but if there is, it eludes me.
The one simple way of doing it doesn't use nested scopes at all,
but uses new.function to construct a function with a new global
dict:
>>> import new
>>>
>>> gg = 1
>>> def plusgg( n ):
... global gg
... print gg+n
...
>>> plusgg(1)
2
>>> new.function( plusgg.func_code, { 'gg': 100 } )(1)
101
>>> gg
1
-- Steve Majewski
More information about the Python-list
mailing list