【发布时间】:2017-06-17 01:09:37
【问题描述】:
我有一个method dispatch decorator,其中包含三个注册函数。一个发送到int,效果很好。在自定义类型上调度的第二个也可以正常工作。第三种也是自定义类型,但 Class 用 lru_cache 装饰器包裹。
(为了让事情更复杂一点,该类通过另一个类的__call__ 方法上的methoddispatch 以迂回的方式实例化。)
@lru_cache(maxsize=None, typed=True)
class QualifiedInterval:
# stuff that works
在 Pitch 类中:
@oph_utils.method_dispatch
def augmented(self, other):
raise NotImplementedError
@augmented.register(int)
def _(self, other):
return "works fine"
@augmented.register(Interval)
def _(self, other):
return "works fine too"
@augmented.register(QualifiedInterval)
def _(self, other):
return "only works if QualifiedInterval class does NOT have lru_cache"
(还有很多事情要做,但这是行不通的部分。)
基本上 - 如果我有 lru_cache,并将 QualifiedInterval 传递给函数,它不会调度并引发 NotImplementedError。如果我注释掉缓存装饰器,它就可以工作。 REPL 上的手动类型检查显示相同的类型(“QualifiedInterval”)。我尝试以几种不同的方式调用创建 QualifiedInterval 的命令,并尝试将其分配给变量。还是不行。我尝试在增强函数中进行显式类型检查。如果启用了缓存,类型检查也会失败。
【问题讨论】:
标签: python python-3.x dispatch lru single-dispatch