I was fooling around with python modules today and i found something interesting; Suppose you have a module like this:
# mymodule
value = "default"
def setVal(new):
value = new
def getVal():
return value
now, you have another module that imports mymodule. when you run the module in python shell, this happens:
>>> moduleC.setC("newvalue")
>>> moduleC.getC()
'default'
>>> moduleC.Cval = "new value"
>>> moduleC.getC()
'new value'
Why does setting the value of the variable in a module using a function not work, but assigning it using "=" does?
In you setVal, value
is local to the function, and 'disappears' when the function returns. To make it global to the module, you must declare it so. This will do as you hoped, even though it is unnecessary (as is getVal()).
def setVal(new):
global value
value = new