module variable scope

Batista, Facundo FBatista at uniFON.com.ar
Tue Oct 28 08:47:20 EST 2003


#- <begin a.py>
#- v = None
#- 
#- def setv( x ):    v = x
#- def getv():    return v
#- <end a.py>
#- 
#- <begin b.py>
#- from a import v, setv, getv
#- 
#- print v
#- setv( 'hello' )
#- print print getv()
#- <end b.py>
#- 
#- 
#- Running b.py gives:
#- None
#- None
#- 
#- 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'.

See 'global' in the documentation.

And, are you sure you don't need a class to do what are you trying to do?

.	Facundo





More information about the Python-list mailing list