singleton objects with decorators

Michele Simionato michele.simionato at gmail.com
Tue Apr 12 08:51:09 EDT 2005


Uwe Mayer wrote:
> "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.

No. Not everybody knows about Singleton. It is an acquired knowledge.
The reason why I am so opposed to Singletons is that a while ago I was
in the condition of not knowing what a Singleton was. However,
everybody
was talking about Singletons, posting recipes of how to implement them
in
Python, etc. So I figured out it was a very important concept and that
I was missing something, since I was incapable of understand its
relevance.
It was only after many months that I understood that there was really
nothing
more to it. I am the kind of person that can learn easily even very
hard
concept, if I can see and use/interest for them. However, I find
extremely
difficult to learn even simple concepts if they do not make sense for
me.
Singleton did not make sense for me since I did not have an use case
for
it. Now, I have some use case for it, but still I think these use cases
are much better managed by memoize. BTW, when I first heard about
memoize,
I understood it instantenously, since I had tons of cases where I had
implemented it in my own code without knowing the technique was called
memoize.

There is so much to know. I don't want to waste my time with concepts
that
are not worth it. IMO, Singleton is not worth the fuss, and we would
be better off without a fancy name for it (just tell what it is: a
class returning always the same instance). Let us reserve fancy names
to
worthy concepts.

In my lectures at Oxford next week I have carefully expunged anything
referring to Singletons ;)

> 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.

IMNSHO, the property "there is only one" is not important enough to
deserves
a name. The property "if you have already called this callable with the
same arguments you don't need to recompute it" instead is quite worthy
and subsumes the other concept as a special case. It also gives you
plenty of use case to illustrate it, even to beginner programmers.


             Michele Simionato




More information about the Python-list mailing list