Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globals and singletons in Python

Tags:

python

I have some few classes which are widespread in my Python application and which should have only one global instance (eg Logger, DbConnection). Python does not support static variables/methods in a class, so the usual Java/C++ way to create a singleton here does not work. I have looked for alternatives to implement singleton in Python. I want a simple (no metaprogramming if possible) and clean implementation. This looks good:

class MyClass(object):
    def a():
        pass

singleton = MyClass()

Using the singleton would be simple as

import myClass
myClass.singleton.a()

The direct assignment could be replaced by a creation function if object initialization is not so simple.

I could also create a getInstance() in module scope and always use it to get myObj.

Question 1) This works ok? The module code (myObj assignment) only runs the first time it is imported into some other module and myObj won't be created every time I import this module somewhere?

An alternative method I have seen is to use a globals module. Something like:

from myClass1 import MyClass1
from myClass2 import MyClass2

myObj1 = MyClass1()
myObj2 = MyClass2()

Using this:

import globals
globals.myObj1.a()

I tend to prefer the first alternative.

Question 2) Between the 2 solutions, what do you recommend?

Question 3) A third solution would be passing the widespread objects such as Logger to several classes/functions, but this is not a good solution imho. Is there a better solution not mentioned here ?

I'm aware of the downsides of using global variables and singletons. However, having a global state is not a big issue in my application. I'll prefer solutions that have code clarity and are simple to use.

like image 432
Alan Evangelista Avatar asked Aug 22 '12 04:08

Alan Evangelista


People also ask

What are singletons in Python?

A Singleton pattern in python is a design pattern that allows you to create just one instance of a class, throughout the lifetime of a program. Using a singleton pattern has many benefits. A few of them are: To limit concurrent access to a shared resource. To create a global point of access for a resource.

What is the difference between global and singleton?

Singleton is used when we do not want to create more than one object. Singleton class ensures that not more than one object is created. However, having a global object doesn't ensure this. This class will not create more than one object.

Are singletons Globals?

The Singleton pattern is basically just a lazily initialized global variable. It ensures that the class has one instance and that instance is globally accessible. However, do not confuse Singleton design pattern with single instances.

Are Python modules singletons?

Modules are “singletons” in Python because import only creates a single copy of each module; subsequent imports of the same name keep returning the same module object.


1 Answers

If you want to have a logger class that only has one instance, just make it a separate module.

# In logging.py
def log(msg):
  print msg

Then from any script you want logging in.

from logging import log
log("A critical error occured.")
like image 111
Aesthete Avatar answered Oct 29 '22 09:10

Aesthete