the singleton pattern def __new__(cls)if not singletonobject instancesingletonobject instance singletonobject __singletonobject(return singletonobject instance def __getattr__(selfname)return getattr(self instancenamedef __setattr__(selfname)return setattr(self instancenamedon' panic at firstit seems like lot is happening here let' walk through the code and see how this results in single instance of class being createdor being returned whenever the class is instantiated the elephant in the room is the class definition inside our singletonobject class the leading underscores tell other programmers that this is private classand they should not be using it outside of the original class definition the private class is where we implement the functionality of the loggerwhich is left to you as an exercise at the end of the for the sake of the examplethis class has an object attribute called val and two methods__init__which instantiates the objectand __str__which is called when you use the object in print statement nextwe see something called class attribute instancewhich is fixed for all objects instantiated from the singletonobject class this attribute is set to nonewhich means that when the script starts to execute there is no value for the instance the other thing we are not used to seeing is the use of the cls parameter and having no self parameter in the definition of the __new__ function this is because __new__ is class method instead of taking the object as parameterit receives the class as parameter and then uses the class definition to construct new instance of the class by now you have seen number of these methods with two leading and two trailing underscores what you need to know is that in the cases you have encountered up to nowthese were used to indicate that these are magic methodswhich are methods used by the python interpreter without your calling them explicitly