super() 返回的不可见属性 class (继承)
Invisible attribute of super()'s returned class (Inheritance)
看看这个使用继承的简单代码:
class A:
def f(self):
pass
class B(A):
def __init__(self):
pass
b = B()
print(dir(b))
正如预期的那样,此列表包括方法 f
,输出为
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'f']
但是现在,请看下面这段代码:
class A:
def f(self):
pass
class B(A):
def __init__(self):
global s
s = super()
b = B()
print(dir(s))
print(s.f)
输出为
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__self_class__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__thisclass__']
不包括函数 f
.
但是,在 运行 行 print(s.f)
之后,输出是 <bound method A.f of <__main__.B object at 0x00000*******>>
所以我的问题是,为什么 s.f
不是 AttributeError,即使 dir(s)
没有说它有 f
attribute/method?
我使用 IPython7.19.0 而不是 python3.9.7
根据文档,
Python dir()
If the object does not provide dir(), the function tries its best to gather information from the object’s dict attribute, if defined, and from its type object. The resulting list is not necessarily complete and may be inaccurate when the object has a custom getattr().
此外,根据文档 Python super()
Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.
The object-or-type determines the method resolution order to be searched. The search starts from the class right after the type.
因此,s.f
被委托给 <bound method A.f of <__main__.B object at 0x00000*******>>
这是对super()
、https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
的精彩解释
看看这个使用继承的简单代码:
class A:
def f(self):
pass
class B(A):
def __init__(self):
pass
b = B()
print(dir(b))
正如预期的那样,此列表包括方法 f
,输出为
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'f']
但是现在,请看下面这段代码:
class A:
def f(self):
pass
class B(A):
def __init__(self):
global s
s = super()
b = B()
print(dir(s))
print(s.f)
输出为
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__self_class__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__thisclass__']
不包括函数 f
.
但是,在 运行 行 print(s.f)
之后,输出是 <bound method A.f of <__main__.B object at 0x00000*******>>
所以我的问题是,为什么 s.f
不是 AttributeError,即使 dir(s)
没有说它有 f
attribute/method?
我使用 IPython7.19.0 而不是 python3.9.7
根据文档, Python dir()
If the object does not provide dir(), the function tries its best to gather information from the object’s dict attribute, if defined, and from its type object. The resulting list is not necessarily complete and may be inaccurate when the object has a custom getattr().
此外,根据文档 Python super()
Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.
The object-or-type determines the method resolution order to be searched. The search starts from the class right after the type.
因此,s.f
被委托给 <bound method A.f of <__main__.B object at 0x00000*******>>
这是对super()
、https://rhettinger.wordpress.com/2011/05/26/super-considered-super/