Classes and modules are singletons?
Micah Cowan
micah at hollister.bcsweb.com
Wed Mar 5 22:38:50 EST 2008
Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> I recall that Python guarantees that module objects are singletons, and
> that this must hold for any implementation, not just CPython: you can
> only ever create one instance of a module via the import mechanism. But
> my google-foo is obviously weak today, I cannot find where the Python
> language reference guarantees that. Can somebody please point me at the
> link making that guarantee?
It's not an absolute strict guarantee; it's just implied by the fact
that "import" uses any appropriate objects already found in
sys.modules.
>>> import sys
>>> ll = []
>>> for i in range(2):
... import string
... ll.append(string)
... del sys.modules['string']
...
>>> ll[0] is ll[1]
False
(I'm sure there are very good reasons never to do what I just did there.)
--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
More information about the Python-list
mailing list