简体   繁体   中英

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?

Because mro is a method of the metaclass and it needs an instance -- ie a class --, pretty much like given an ordinary class C and a method m you can call Cm(inst) or inst.m() , but you can't call Cm() , as it expects the self argument.

If you want to call mro with a metaclass or type itself, you can use type.mro(type) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM