Assigning to self
newsgroups at jhrothjr.com
Mon Jan 17 15:03:25 EST 2005
"Frans Englich" <frans.englich at telia.com> wrote in message
news:mailman.805.1105987092.22381.python-list at python.org...
> What the code attempts to do is implementing a, to the API user,
> memory-saver by ensuring that no more than one instance of the class foo
> exists for a particular id. E.g, the user can simply "create" an instance
> if one not already exists, it is created.
In other words, you're trying to create a singleton. In general,
singletons are frowned on these days for a number of reasons,
not least because of the difficulty of testing them.
> First of all; am I approaching the goal with the right solution?
No. In all Python releases since 2.2, the correct way of doing this is to
use the __new__() method. Unfortunately, the only place it is documented
The first reference contains an example of how to do
a singleton: simply search on the word Singleton.
More information about the Python-list