module variable scope

Alex Martelli aleax at aleax.it
Wed Oct 29 04:56:58 EST 2003


Stefan Quandt wrote:

> "Batista, Facundo" <FBatista at uniFON.com.ar> wrote in message
> news:<mailman.170.1067348898.702.python-list at python.org>...
>> > I expected variable v to be changed by calling setv() but it is not.
>> > Who can explain this?
>> 
>> No, because you're creating a new 'v' variable in the *local* scope of
>> 'setv'.
> So how can I access the global module variable v within function
> setv() (like getv() does automatically)?

Accessing is easy, but you're talking about re-binding, which is
quite a different thing.

Integers (and floats, strings, tuples) are immutable.  So, there
is no way you can mutate the object to which name 'v' is bound
while leaving name v itself alone.  All you can do is to re-bind
name 'v' -- have it refer to a new and separate object from the
one it was previously referring to; other names (in the same or
other modules) referring to that previous object will of course
be totally unaffected by the vagaries of this name 'v' (there
is no way in which rebinding one barename can affect the binding
of other separate barenames).


Alex





More information about the Python-list mailing list