On most types/classes in Python, I can call .mro()
without arguments. But not on type
and its descendants:
In [32]: type(4).mro()
Out[32]: [int, object]
In [33]: type(type(4)).mro()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-48a6f7fcd2fe> in <module>()
----> 1 type(type(4)).mro()
TypeError: descriptor 'mro' of 'type' object needs an argument
It appears I can get what I want with type(type(4)).mro(type(4))
, but why can't I call mro()
directly as I do elsewhere?
Because mro
is a method of the metaclass and it needs an instance -- i.e. a class --, pretty much like given an ordinary class C
and a method m
you can call C.m(inst)
or inst.m()
, but you can't call C.m()
, as it expects the self
argument.
If you want to call mro
with a metaclass or type
itself, you can use type.mro(type)
.
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