0

code goes first:

def singleton(cls):
    instances = {}
    def get_instance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return get_instance

@singleton
class A:
    #...

Ok, the code above is an implementation of Singleton, I saw this implementation in another post.

I don't understand why the singleton function returns a function but A is a class. How does it worK?

Community
  • 1
  • 1
Alcott
  • 17,905
  • 32
  • 116
  • 173

2 Answers2

1

When you call MyClass() after it's been decorated, you're right -- you're not actually calling a class, you're calling a function.

That function calls the class if cls not in instances and caches it, then returns the cached instance.

In other words, there is no reason MyClass() has to call the class directly -- it will work as expected as long as it returns an instance of the class.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Yep, it works fine. It saves a single instance and returns it whenever you call `MyClass()`. – agf Sep 20 '11 at 00:00
  • True, but most of the time that doesn't matter -- you generally shouldn't need to check type in Python, you just use it, and it will give an error if it doesn't support the correct interface. – agf Sep 20 '11 at 00:01
  • @agf, true. Although a few others things break like pickles. Its just enough that I'm slightly uneasy about replacing a class with a function. – Winston Ewert Sep 20 '11 at 00:13
  • @WinstonEwert I agree, there are better implementations (many in the Python Singleton" questions here) if you really need a singleton. – agf Sep 20 '11 at 01:02
1

A isn't a class in the end. The class A gets created, but then replaced with the function that singleton returns. So in the end, A ends up being a function.

But since you call a class to create a object, it ends up working pretty much the same way. But isinstance won't work.

P.S. you probably shouldn't use a singleton. In python it is almost always the wrong choice.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • Why shouldn't I use a singleton? What if I implement the Singleton Design Pattern using __new__? – Alcott Sep 20 '11 at 00:00
  • @Alcott search the site for Python Singleton, or read the questions in the "Related" list. It's been addressed many times. – agf Sep 20 '11 at 00:04
  • @Alcott A singleton is just the same as a global variable. If you really need one just instantiate your singleton class once into a global variable instead of messing with this stupid caching hack, and replace everywhere `MySingletonClass()` with `MySingletonInstance`. Singletons are often a bad idea for the same reasons that globals are often a bad idea, and if your code *looks* like it's instantiating a new instance rather than referencing a global variable then it's **even harder** to figure out the secret communications channels between parts of your code that don't directly communicate. – Ben Sep 20 '11 at 00:09
  • 4
    @Alcott, if your singleton object has state, then you've created a global variable. If your singleton object does not have state then you've created a module. Either way using a singleton hasn't helped anything. See http://www.youtube.com/watch?v=-FRm3VPhseI for a demonstration of how to properly avoid global state. – Winston Ewert Sep 20 '11 at 00:11
  • @Winston, thanks for your link, but I'm not able to watch it, because I'm in China, :P – Alcott Sep 20 '11 at 00:18