My main problem with this method is that PyQt doesn’t allow for __metaclass__ because sip already defines a __metaclass__ for all PyQt objects. Otherwise this method seems fairly clean (Although metaclasses do seem to confuse many python users)

This one has a fairly glaring issue, and I don’t know why so many people recommend it.. This variant ends up with __init__ being called EVERY time you access it via Singleton(), which causes all sorts of issues. (You could track if __init__ has already run, but that’s even more crap your Singleton has to hack around in your class)

Creating a wrapper class (Too long to post here, here’s the recipe link):

Well that’s just not what most people want… if you don’t have access to where the current instance is stored, you now have to understand how the singleton is implemented and access Singleton.__single in order to get it… Yuck.
It also means you have to check whenever you need it for an exception.

I like how simple this variant is, it certainly seems closer to what I want. The issue with this singleton is that it can’t be subclassed, but otherwise it seems like fairly elegant python.
(You could subclass by using Singleton.__class__ I suppose, but that means other people subclassing you have to be aware of it. And their class won’t automatically behave like a singleton without them also overwriting their own name as well)

And there you have it (If you want it anyways…)
No issues with subclassing, since at declaration time (And all the way up until Singleton() is called) your object is still the class, not the instance.
init is only called once, as it should be. And no metaclass is used, so PyQt is happy.

Yeah it would result in something like that. Type comparisons after instantiation wouldn’t be possible in that way due to the class no longer being present in that name. In my use case, i really don’t care about that, but if you needed to type check I could see it being an issue.

I really dislike not using the constructor, and __init__ raising an Exception just bothers me… If there was a (clean) way of doing a module level property, it might be acceptable to do it that way, but from what I can tell there’s no elegant solution to that (Just hacking sys.modules).

Honestly, I have no idea why everyone complains so much about Singletons in python.

1) They should only be used rarely. Rarely!
2) Don’t bother creating a subclass to encourage their use or save 5 lines of clear code.
3) There are 10 ways to create singletons, all perfectly good (even if Singletons are bad), all of them are trivial to test and unlikely to be a source of any bug whatsoever.

Singletons in python are, to me, a terrible form of framework masturbation.

kingmax said: 2013.08.17 01:29

gui application need singleton, for example PyQt in maya

manuel said: 2013.09.04 05:33

Hi Nathan,
I use your solution to the singleton pattern frequently.
But now I encountered a new situation where I want my singleton class to inherit from ‘dict’ rather than from ‘object’. I’m unsure how to modify the code – somehow, somewhere I need to initialice the dict – but how. Any chance you could clarify that.
Regards,
Manuel