__call__ method of type class

Viewed 6100

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?

2 Answers
Related