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

Andy Gimblett gimbo at ftech.net
Fri Apr 5 12:04:00 EST 2002


On Fri, Apr 05, 2002 at 10:44:00AM -0600, Gustavo Cordova wrote:

> Andy hadn't caught on that a module is loaded only once,
> the first time it's imported. Any subsequent imports are

To be fair to myself, that's not strictly true.  I did know that, I
just hadn't made the connection between that fact and the implication
that the module was, thus, actually, in fact, a singleton.  :-)

> 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?

It really is.  My biggest problem with my python programming is
definitely a tendency to overcomplicate things.  :-/

-Andy

-- 
Andy Gimblett - Programmer - Frontier Internet Services Limited
Tel: 029 20 820 044 Fax: 029 20 820 035 http://www.frontier.net.uk/
Statements made are at all times subject to Frontier's Terms and
Conditions of Business, which are available upon request.





More information about the Python-list mailing list