【发布时间】:2021-05-04 05:26:46
【问题描述】:
我认为 super() 总是会尝试使用基类中的方法(或属性)运行,直到我遇到一个使用super().__getattribute__("foo") 获取子类属性的用例。简化代码如下所示。
class Base:
value = "base"
def foo(self):
print(f"Base foo, {self.value}")
class Derived(Base):
value = "derived"
def foo(self):
print(f"Derived foo, {self.value}")
def bar(self):
print(f"Derived bar {self.value}")
print(super().value)
print(super().__getattribute__("value"))
super().foo()
super().__getattribute__('foo')()
d = Derived()
d.bar()
输出,
Derived bar derived
base
derived
Base foo, derived
Derived foo, derived
有点超出我之前的理解,只有__getattribute__是个例外吗?这个doc我无法获得更多细节,希望有人能帮助我更清楚地理解这一点,谢谢!
编辑关注__getattribute__问题,如下:
class Base:
value = "base"
def foo(self):
print(f"Base foo, {self.value}")
def __getattribute__(self, k):
print(f"Base get attr {self}, {k}")
return super().__getattribute__(k)
class Derived(Base):
value = "derived"
def foo(self):
print(f"Derived foo, {self.value}")
def __getattribute__(self, k):
print(f"Derived get attr {self}, {k}")
return super().__getattribute__(k)
def bar(self):
print("Derived bar")
print(super().value)
print(super().__getattribute__("value"))
d = Derived()
d.bar()
输出是:
Derived get attr <__main__.Derived object at 0x7fb0621dba90>, bar
Base get attr <__main__.Derived object at 0x7fb0621dba90>, bar
Derived bar
base
Base get attr <__main__.Derived object at 0x7fb0621dba90>, value
derived
【问题讨论】:
-
它总是获取基类中的属性/方法(而是方法解析顺序中的下一个类)。你认为
super哪里没有这样做,究竟是什么? -
也许,这将揭示:
object.__getattribute__(d, 'value') -
最近有人发了一个非常相似的问题:stackoverflow.com/questions/67377122/…
-
@juanpa.arrivillaga:我一直以为
super()实际上返回了一个基类的object。很长一段时间我才明白,它只是为属性查找一次的代理。 -
@SergeBallesta 你从哪里得到这个
it was only a proxy to look once for an attribute?我认为让我感到困惑的是为什么super().xxx不调用object.__getattribute__(d, 'xxx')
标签: python python-3.x super