Reading Variables From Modules ?

Alex Martelli aleax at aleax.it
Tue Sep 11 08:45:11 EDT 2001


"Peter Moscatt" <pmoscatt at bigpond.net.au> wrote in message
news:_Zln7.36425$bY5.192519 at news-server.bigpond.net.au...
> Alex & Pete,
>
> Thanks for the replys.  Yep, I tried it and all is well.
>
> Is this the best way to handle transfers like this or is there a prefered
> method ?

The most general and flexible way to package some data
("state") along with some code ("behavior") is generally
to define a class.  In some cases you need to ensure all
instances of the class always have identical state (this
is often misconstrued as a need to ensure there is only
one instance -- the so-called "singleton" design pattern),
in which case you may want to try my "Borg" idiom (name
courtesy of David Ascher!-) which I posted as recipe
"Singleton? We don't need no stinkin' singleton!" at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531
but this need for state-uniqueness is not as frequent
as all that.  And sometimes you don't really need the
generality and flexibility!  Then, "do the simplest
thing that could possibly work" -- and using module
variables (aka "global" variables from the point of
view of the module's functions) is simple indeed, so,
if it can indeed work for your needs, go for it!-)

(Just do make sure that even simpler approaches, such
as using function arguments and return values, can't
also work... then, DO choose the simplest one!-).


Alex






More information about the Python-list mailing list