singleton objects with decorators

Uwe Mayer merkosh at hadiko.de
Tue Apr 12 10:19:00 EDT 2005


Tuesday 12 April 2005 14:51 pm Michele Simionato wrote:

> No. Not everybody knows about Singleton. It is an acquired knowledge.

Well, what isn't? 
What I ment to say, but failed to do so more explicitly, was that it is a
term I felt which was generally known to "the programming society". Or that
it is a term that might pop up in a comunity where software design is an
issue.
This of course does not mean that you could not have used it without knowing
that someone had thought of a fancy name for it. 

[...]
> It was only after many months that I understood that there was really
> nothing
> more to it. 

I agree. There really isn't much to Singletons. There was a time when I also
did not know about Singletons or design pattern in general. But thats
always the case somehow, somewhere. 

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

Perhapts you shouldn't. Otherwise people might stumble over the same problem
you did. 

> IMNSHO, the property "there is only one" is not important enough to
> deserves
> a name. 

The problem here is: it has already a name. It is the probably most simple
pattern there is. And if only it serves as a mind-boggingly example of a
design pattern, why not call it a "singleton". Why call it a "class that
can only be instanciated once"?
I do not yet feel there is being made too much fuss about it. People do not
start name-dropping on singletons. Design pattern in general, perhaps. 

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

To step in your argument, you could also call that "caching a function
call".

BTW: @memoize on the __new__ method isn't quite enough. You'll have to call
it on __init__ as well, otherwise it is executed again on the already
initialised object.
Also calling @memoize on the __init__ does not suffice either, because can
call it with different parameters...

Ciao
Uwe     



More information about the Python-list mailing list