From all I understand about Python object oriented programming if a class has __call__ method defined that would be invoked if we use the instance of the class like a function call. For example:
class Employee:
    def __init__(self,name,sal):
        self.name = name
        self.salary = sal
    def __call__(self,value):
        return self.salary * value
e = Employee("Subhayan",20000)
print (e(10))
So the __call__ method takes the object instance as the first argument.
I am just trying to understand metaclasses in Python and I read that type is the default metaclass of all user defined classes.
If I define a basic custom metaclass in Python:
class Meta(type):
    def __new__(meta, classname, supers, classdict):
        # Run by inherited type.__call__
        return type.__new__(meta, classname, supers, classdict)
Now as per the documentation given in the book the metaclass __new__ method would be run by the __call__ method inherited from type. 
Now my question is to use the __call__  method of any class we have to have an object of that class and then call it as a function.
Here we don't have any object of type to use its __call__ function.
Can someone please explain to me how is the __call__ function of type class coming into picture?
Any class is itself an instance of type "type" - therefore "calling" a class just calls the method __call__ on its class - which happens to be type's __call__.
The effect of type.__call__ is exactly: 
on code like:
class A:
    pass
b = A()
type.__call__ receives the class A itself as its first parameter. A.__new__ - in pseudocode we could write instace = A.__new__(cls) as what runs.  __init__ on the instance(instance.__init__())return instance
However, if the the class itself is derived from "type" - i.e., it is the instantiation of a "new" class, extra steps are taken:  The special value of the magic __class__ variable is filled in any of the class methods - if any of those methods use a call to super. And on Python 3.6, these 2 further steps: any of the class attributes that define a  __set_name__ method are called, and the class's __init_subclass__ method is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With