There's GOT to be a better way!
Earl Eiland
eee at nmt.edu
Thu Mar 3 18:54:37 EST 2005
On Thu, 2005-03-03 at 16:46, Steven Bethard wrote:
> 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
Guess I'm just gonna have to break down and figure out how to work with
classes!
More information about the Python-list
mailing list