pythoninheritancemetaclassmethod-resolution-ordersix

Python type() not giving exact class type instead gives metaclass type


I am trying to pass the type of a class to a method so that it can be dynamically instantiated. The class extends to a base class which further extends to an abstract class. Now when I check the type of my class it comes as the abstract class type instead of the child class.

Here is how my classes look like

class AMeta(type):
     # stuff

class Parent(six.with_metaclass(AMeta, object)):
     # stuff

class Child(Parent):
    # stuff

Now when I use type(Child) or Child.__class__ it gives me AMeta whereas I would like to get Child. I want to pass this Child to another method which would dynamically create its object.

def create_obj(clzz):
   return clzz()

When I call the method like create_obj(type(Child)) it doesn't work and breaks but when I call Child.mro()[0] it works fine what is happening here and is there another way to achieve what I am achieving via mro method?


Solution

  • A class is an instance of its metaclass. Ergo: