functools.singledispatch
有助于定义单调度泛型方法。同时,还有super()
调用方法或访问超类的属性。
有没有类似的东西super()
可以用singledispatch
?我尝试了以下方法,但结果super(Derived, value)
只是不是实例Base
,所以它没有按我预期的那样工作:
from functools import singledispatch
@singledispatch
def hello(value):
return ['default']
@hello.register(Base)
def hello_base(value):
return hello(super(Base, value)) + ['base']
@hello.register(Derived)
def hello_derived(value):
return hello(super(Derived, value)) + ['derived']
print(hello(Derived())
# expected ['default', 'base', 'derived'],
# but actually is ['default', 'derived'].