There's GOT to be a better way!
Steven Bethard
steven.bethard at gmail.com
Thu Mar 3 18:46:57 EST 2005
Earl Eiland wrote:
> module.py
> def A():
> test = 1
> for x in range(10): B()
>
> def B():
> test = test + 1
>
>
> main.py
> import module
> module.A()
>
> This will fail, unless test is passed and returned.
(Sorry if this sent twice. It wasn't appearing for me the first time.)
You can use global here, though I wouldn't advise it.
---------- module.py ----------
def A():
global test
test = 1
for x in range(10):
B()
def B():
global test
test = test + 1
-------------------------------
py> import module
py> module.A()
py> module.test
11
This looks like it might be simpler with a class, e.g.:
---------- module.py ----------
class A(object):
def __init__(self):
self.test = 1
for x in range(10):
self.B()
def B(self):
self.test += 1
-------------------------------
py> import module
py> a = module.A()
py> a.test
11
STeVe
More information about the Python-list
mailing list