Globals and loners in Python

I have several classes that are widespread in my Python application and which should only have one global instance (e.g. Logger, DbConnection). Python does not support static variables / methods in a class, so the usual Java / C ++ way of creating a singleton does not work here. I was looking for alternatives to implement singleton in Python. I want a simple (without metaprogramming, if possible) and clean implementation. It looks good:

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

Using a singleton would be as simple as

 import myClass myClass.singleton.a() 

The direct assignment can be replaced by the create function if the initialization of the object is not so simple.

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

Question 1) Does it work fine? Module code (destination myObj) is only triggered the first time I enter any other module, and myObj will not be generated every time I import this module somewhere?

An alternative method I've seen is to use the global module. Sort of:

 from myClass1 import MyClass1 from myClass2 import MyClass2 myObj1 = MyClass1() myObj2 = MyClass2() 

Using this:

 import globals globals.myObj1.a() 

I prefer the first alternative.

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

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

I know about the disadvantages of using global variables and singletones. However, having a global state is not a big problem in my application. I prefer solutions that have code clarity and are easy to use.

+6
source share
3 answers

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

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

Then from any script you want to log in.

 from logging import log log("A critical error occured.") 
+6
source

Consider creating the following:

  • State class
  • Proxy class

The state class will create a stream that will be initialized in the init .py module. The state class will have methods that pass instances of the class to the stream and store the weakref value in it.

Later, a proxy class can be used to access instances of the class in the stream. If class objects are not created, the proxy will create an instance of the desired class and save it in the stream.

Using this template, you can limit the number of instances of each class to 1 and ensure that whenever any part of your application is running, any instance that needs to be used globally does not need to be recreated.

0
source

The correct answer to the question of how to make single games? Not. You must explicitly pass the link to everything you need to use. You can reduce effort with factory functions, wrapper classes, etc.

Even if there is something, such as a screen or a log, that is global in nature, you should still pass it explicitly to allow unit testing. Please note that this only applies to things that should be part of the final design. If your logger is just a quick hack for debugging, feel free to make it global.

-1
source

Source: https://habr.com/ru/post/923446/


All Articles