[Python-Dev] Re: Python-Dev Digest, Vol 8, Issue 91 (original) (raw)

Edward Loper edloper at gradient.cis.upenn.edu
Tue Mar 30 10:09:24 EST 2004


Hi folks.

Earlier messages suggested a nice singleton decorator, which is shown in the draft PEP: [...] But i don't think this is a good way to write a singleton, because then the user of the class has to get instances by saying "MyClass" instead of "MyClass()".

Depending on how you look at it, that might be a good thing. In particular, it "documents" the fact that you're using a singleton, which might make your code easier to read/understand. (You expect very different behavior from a singleton than from a normal class).

On a related note, now that Python has class methods, is there much point in a "singleton" pattern? In particular, why not just make a class that only defines class methods, and uses the class namespace to store variables (instead of an instance namespace)?

-Edward



More information about the Python-Dev mailing list