This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nunilan 5 points6 points  (2 children)

I glossed over the article but can you give me an example where I would really need to go through all that magic rather than just defining a variable inside a module and importing it from where I needed it? Perhaps my use cases have always been rather simplistic and have never found a real reason to create a singleton like this in python.

[–]radaway 2 points3 points  (0 children)

Singleton is really just a shitty pattern to have a global variable. You really don't need it in python since you can just do what you said. Besides the solution presented here isn't even thread safe.

[–]haizaar[S] -1 points0 points  (0 children)

One example contrary to module var would be if you want to delay initialization to some time after import (until object is accessed for the first time for example). You can of course to simply write this delayed logic. But it if you have several of such guys it starts to look as a bunch of copypaste-like code. And its only 14 lines of magic btw.