From: brian z. <bz...@zi...> - 2001-04-23 12:40:44
|
Paolo, The problem is you have not defined Count to be global in set() so a lccal variable called Count is being set with the value of newrc, not the top level variable as you expected. To fix it: def set(newc): global Count Count = newc However, this is considered very bad practice. Instead, try to build a class which contains the state, such as: class Counter: def __init__(self): self.count = 0 c = Counter() print c.count c.count = 5 print c.count While this is a bit bigger step conceptually, it will pay dividends as the scoping will be much easier to maintain. hope this helps, brian > > Hi. > > I have some problem to set a moduel variable into a function. > > I worte the following python module: > > ##Module mymodule.py > > Count = 0 > > def get(): > return Count > > def set(newc): > Count = newc > > > >From prompt I type: > >>> import mymodule > >>> mymodule.get() > 0 > >>> mymodule.set(5) > >>> mymodule.get() > 0 > > I was expecting 5 ! > > Anyone knows my mistake ? > > Thank you in advance. > Paolo. |