I decided to read about magic method again today. It is always good to revisit and refresh the learning.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__).

What I want to focus here for magic methods that gets executed when an object is initialized from a class.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/​

Now why I wanted to talk about the magic method in relavance to those that get executed everytime a class is initialized is because I wanted to pen down some learning about Singleton pattern.

__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.

While researching I also came across a SO link that talks about why BORG pattern is better than Singleton pattern.
​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).

Furthermore, a class basically describes how you can access (read/write) the internal state of your object.In the singleton pattern you can only have a single class, i.e. all your objects will give you the same access points to the shared state. This means that if you have to provide an extended API, you will need to write a wrapper, wrapping around the singletonIn the borg pattern you are able to extend the base "borg" class, and thereby more conveniently extend the API for your taste.

Now are you with me, lets dig even more further into metaclasses

​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()

You've seen that type lets you do something like this:

MyClass =type('MyClass', (), {})

​It's because the function type is in fact a metaclass. type is the metaclass Python uses to create all classes behind the scenes.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

This site contains actual content, written by an actual person. All of this stuff is mine. Even though it may read like it was written by monkeys on typewriters, I actually wrote it. If you want to use my stuff, you can't. At least, not without contacting me first. I also have a privacy policy that I guard fiercely. Any information you provide on this website will not be used by anyone else but me.
Blog, Website Content, VFX Showreel work & Tools & Scripts by Sanjeev Kumar is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

>

Powered by Create your own unique website with customizable templates.