Assigning to self

John Roth 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...
>
> Hello,
>
[...]

>
> What the code attempts to do is implementing a, to the API user, 
> transparent
> 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 
> and
> 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
is here:

http://www.python.org/2.2.3/descrintro.html

and here:

http://users.rcn.com/python/download/Descriptor.htm

The first reference contains an example of how to do
a singleton: simply search on the word Singleton.

John Roth

> Cheers,
>
> Frans
>
>
>
> 




More information about the Python-list mailing list