Generic singleton

mk mrkafk at gmail.com
Wed Mar 3 16:11:40 EST 2010


Arnaud Delobelle wrote:
> mk <mrkafk at gmail.com> writes:
> [...]
>> hashable
>> ..
>>     All of Python’s immutable built-in objects are hashable, while no
>> mutable containers (such as lists or dictionaries) are.
>>
>> Well ok, hashable they're not; but apparently at least dict and list
>> have id()?
> 
> lists and dicts are not hashable, but their types are:

Oh.

>>>> hash(dict)
> 4296155808
>>>> hash(list)
> 4296151808
> 
> So just use the type as the key to the dictionary you maintain in your
> singleton.  

Hmm I have tested it and it seems to work.

> But the question is what you use your singleton for.

Well, partly this was for sake of learning; but partially I was thinking 
whether singleton as such may be useful for implementing "global queue 
of tasks" for worker threads in a large program: instead of handing down 
over and over the same reference to the global queue of tasks, classes 
or functions could call Singleton() and be done with it.

Or I could make my life simpler and use global variable. :-)

I will soon need to write such program, with remote calls from pyro 
(most probably), many worker threads, short-running jobs and 
long-running jobs, etc.


Regards,
mk




More information about the Python-list mailing list