They're special methods that you can define to add "magic" to your classes. They're always surrounded by double underscores (e.g. __init__ or __lt__).
We all know the most basic magic method, __init__. It's the way that we can define the initialization behavior of an object. However, when I call x = SomeClass(), __init__ is not the first thing to get called. Actually, it's a method called __new__, which actually creates the instance, then passes any arguments at creation on to the initializer. At the other end of the object's lifespan, there's __del__. if you want to read more in detail checkout Refe Kettler's guide to python magic methods: https://rszalski.github.io/magicmethods/
class Singleton(object): _instance = None # Keep instance reference # why do this???? If do this, the if state follow is always false, right? def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = object.__new__(cls, *args, **kwargs) print("creating") return cls._instance a = Singleton() b = Singleton() print(a is b)
__new__ is a class method as well that supersedes the __init__ method (you have control on the object which gets created at this level)
_instance is a class attribute, not an instance attribute. So it's visible/available in the __new__ method before an instance is created.
So first time, the cls._instance argument is None, so __new__ creates the instance and stores the result in the _instance class attribute of cls (which is your Singleton class)
It is not None the second time because the reference of the instance has been stored, so it returns the same self._instance object. In the process, object.__new__ is only called once during the lifetime of the class.
This is the design pattern of the singleton: create once, return the same object every time.
https://stackoverflow.com/q/1318406/9567948 . The first answer gives the best and simplest explanation which is
If you subclass a borg, the subclass' objects have the same state as their parents classes objects, unless you explicitly override the shared state in that subclass. Each subclass of the singleton pattern has its own state and therefore will produce different objects.
Also in the singleton pattern the objects are actually the same, not just the state (even though the state is the only thing that really matters).
In the borg pattern you are able to extend the base "borg" class, and thereby more conveniently extend the API for your taste.
What are metaclasses?
Metaclasses are the 'stuff' that creates classes.
You define classes in order to create objects, right?
But we learned that Python classes are objects.
Well, metaclasses are what create these objects. They are the classes' classes, you can picture them this way:
MyClass = MetaClass() y_object = MyClass()
MyClass = type('MyClass', (), {})
Now you wonder why the heck is it written in lowercase, and not Type?
Well, I guess it's a matter of consistency with str, the class that creates strings objects, and int the class that creates integer objects. type is just the class that creates class objects.
You see that by checking the __class__ attribute. Everything, and I mean everything, is an object in Python