singleton objects with decorators
Uwe Mayer
merkosh at hadiko.de
Tue Apr 12 07:16:51 EDT 2005
Tuesday 12 April 2005 12:09 pm Michele Simionato wrote:
> Steven Bethard:
>> It strikes me that I've never wanted or needed a singleton object.
>> Would you mind sharing your use case? I'm just curious.
>
> "Singleton" is the most idiotic pattern ever. If you want an instance,
> just
> instantiate your class once. If a class should have only one instance,
> you can just document it. What I find usuful is "memoize", which
> contains "Singleton" as a special case. So I use memoize even
> for singleton would-be, i.e. logfiles and databases connections
> (memoizing the connections, if I try to open a database twice with the
> same parameters, I am returned an instance of the already opened
> database).
"Singleton" is simple (like the wheel), but that does not make it stupid.
There are two aspects that are important:
1. a Singleton has one, very simple property and virtually everyone knows
what you talk about when you explain that you used a "Singleton". In this
case its just a technical term. We need technical terms.
2. the property of a Singleton, i.e. there is only one, is important - you
use it yourself through memoize. That is just a more flexible
implementation of having one instance of whatever you memoize.
Using @memoize on the __new__ method works very well and is flexible enough
to be used with any function call. Thanks for the tip.
Ciao
Uwe
More information about the Python-list
mailing list