[Tutor] Another question on global variables...

Magnus Lycka magnus@thinkware.se
Thu, 17 Oct 2002 15:17:33 +0200


At 09:47 2002-10-17 -0400, andy surany wrote:
>ok, I think I'm straight on globals. Thanks everyone.

Great!

>Alan - regarding your OO example, if I have to assign the result to an
>external variable, isn't that the same as a global? Seems like I'm kinda
>defeating the purpose of using a function over a global. Or have I
>misunderstood?

The point is to contain variables to the smallest
possible namespace. I'm not sure what you refer to
here. The variable called "self.abc" only exists
inside one instance of the ABC class.

The variable name "abc" or even "self.abc" can be
used for other stuff in other places of the code
without getting mixed up.

Or are you referring to storing the result of
foo.get_it() somewhere? As long as foo exists,
you don't need to store it, and where ever you
use/store it, should typically be in another
local namespace.

 >>------- OO Example ------
> >
> >class ABC:
> >   self.abc =3D []
> >   def set_it(self): self.abc =3D [1,2]
> >   def get_it(self): return self.abc
> >
> >foo =3D ABC()
> >foo.get_it()  # prints initial empty list
> >foo.set_it    # sets new list
> >print foo.get_it()  # prints new value
> >---------------------------




--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se