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?

1
  • @Jim, Sorry, I will be more careful. Commented Sep 19, 2011 at 23:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Why shouldn't I use a singleton? What if I implement the Singleton Design Pattern using new?
@Alcott search the site for Python Singleton, or read the questions in the "Related" list. It's been addressed many times.
@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.
@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 youtube.com/watch?v=-FRm3VPhseI for a demonstration of how to properly avoid global state.
@Winston, thanks for your link, but I'm not able to watch it, because I'm in China, :P
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.

4 Comments

Yep, it works fine. It saves a single instance and returns it whenever you call MyClass().
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, true. Although a few others things break like pickles. Its just enough that I'm slightly uneasy about replacing a class with a function.
@WinstonEwert I agree, there are better implementations (many in the Python Singleton" questions here) if you really need a singleton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.