Modules are Singletons! (was Re: Catalog of Python Patterns? [LONG!])

Gustavo Cordova gcordova at hebmex.com
Fri Apr 5 11:44:00 EST 2002


> 
> Andy Gimblett <gimbo at ftech.net> writes:
> > > Use a module.  Don't use a class unless you need a class.
> > 
> > /me closes his eyes tight, opens his mouth, thinks about it for a
> > while, shaking his head around slightly, feels something click, says
> > "wow", opens his eyes, says "wow" again, and hits reply.
> > 
> > Wow.  Man, I love python.
> > 
> > Cheers for the epiphany
> 
> Care to explain it?  I missed something.
> 

Andy hadn't caught on that a module is loaded only once,
the first time it's imported. Any subsequent imports are
only accesses to the original imported version, so that
makes them effectively singletons.

So if you keep lotsa utility functions, variables and
constants in a certain module, and import it from a lot
of diferent places in a single application, then you're
really referencing the originally imported version, all
others are only references.

Neat, eh?

:-)

-gustavo





More information about the Python-list mailing list