【发布时间】:2014-08-29 04:06:52
【问题描述】:
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'].
【问题讨论】:
标签: python python-3.x super method-resolution-order functools