为什么 super 只有 class 个属性的子集?
Why does super only has a subset of class attributes?
假设我在 class 之外使用 super
。这个我可以。
super(int, 1).__str__ # <method-wrapper '__str__' of int object at 0x000000005E29C6B0>
但是我做不到
super(int, 1).__float__ # raises an AttributeError
更一般地说,我可以检查 1
拥有的所有方法,但不包括 super(int, 1)
对象。
set(dir(1)) - set(dir(super(int,1)))
这会输出一组特定于 1
的四十多个属性,即 __round__
、__float__
或 real
,仅举几例。
是什么原因导致 super
and/or 这些方法有何不同。
当您执行 super(int, 1).__float__
时,您是在请求方法 int.__float__
覆盖。一个都没有。
假设我在 class 之外使用 super
。这个我可以。
super(int, 1).__str__ # <method-wrapper '__str__' of int object at 0x000000005E29C6B0>
但是我做不到
super(int, 1).__float__ # raises an AttributeError
更一般地说,我可以检查 1
拥有的所有方法,但不包括 super(int, 1)
对象。
set(dir(1)) - set(dir(super(int,1)))
这会输出一组特定于 1
的四十多个属性,即 __round__
、__float__
或 real
,仅举几例。
是什么原因导致 super
and/or 这些方法有何不同。
当您执行 super(int, 1).__float__
时,您是在请求方法 int.__float__
覆盖。一个都没有。