Example for singleton decorator pattern in python

I know there is not very common in Python to use the singleton pattern, but I found a nice implementation of this pattern in Python 3 Patterns, Recipes and Idioms book. Starting with that example I implemented an equivalent of the well known PHP getConnection example.

You have the code below:

This is the class that implements the Singleton pattern.

Now, we create a class and we decorate it with the Singleton class. Let’s import also MySQLdb module *.

Let’s test this:

You will see something like:

As you can see there is only one object.

For fun, let’s remove the line “@Singleton” and re-run the example. This time you will see different objects:

You can find the fully example here.

* If you don’t know how to install MySQLdb, you can check the previous post.

2 thoughts on “Example for singleton decorator pattern in python

  1. Anonymous

    Cool. This makes a lot of sense in cases like this one where the singleton class’s constructor takes no args.

    But when the constructor does take arguments, I find it sketchy that this implementation will ignore the arguments that are passed to subsequent invocations of the class constructor and instead give you the singleton instance that was created with whatever args/ kwargs were used by the first person who constructed it.

  2. razvan Post author

    If I understood correctly your remarks:

    1. Of course you can pass arguments to the constructor:

    class Database:
    def __init__(self, dbname):
    self.dbname = dbname


    db1 = Database(‘my_database_name’).get_connection()

    2. I think that’s the idea of singleton. One instance (the first one) is used all the time across the system: “This is useful when exactly one object is needed to coordinate actions across the system. ” http://en.wikipedia.org/wiki/Singleton_pattern


Leave a Reply

Your email address will not be published. Required fields are marked *