Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does .mro() on a metaclass have a different signature? `descriptor 'mro' of 'type' object needs an argument`

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?

like image 570
gerrit Avatar asked Dec 18 '15 15:12

gerrit


1 Answers

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).

like image 128
Pedro Werneck Avatar answered Nov 02 '22 02:11

Pedro Werneck