class Meta(type):
def __new__(cls, name, bases, dct):
new_class = type(name, bases, dct)
new_class.attr = 100 # add some to class
return new_class
class WithAttr(metaclass=Meta):
pass
print(type(WithAttr))
# <class 'type'>
Why does it print <class 'type'>, but not <class '__main__.Meta'>
Am I right that class WithAttr is instance of Meta?
This is because you're making an explicit call to
type(name, bases, dct), which in turn callstype.__new__(type, name, bases, dct), with thetypeclass passed as the first argument to thetype.__new__method, effectively constructing an instance oftyperather thanMeta.You can instead call
type.__new__(cls, name, bases, dct), passing the child class as an argument to contruct aMetainstance. In case__new__is overridden in a parent class that is a child class oftype, callsuper().__new__instead oftype.__new__to allow the method resolution order to be followed.Change:
to:
Demo: https://ideone.com/KIy2qG