如何解决子类上的 singledispatchmethod 的解决方法
那当然是true
:
# foobar.py
import functools
class FooBar:
@functools.singledispatchmethod
def dispatch_this(self,thing):
raise NotImplementedError
# foo.py
import foobar
class FooThing:
pass
foobar.FooBar.dispatch_this.register(FooThing,lambda x,y: f"Dispatching {y.__class__.__name__} from {x.__class__.__name__}")
# bar.py
import foobar
class BarThing:
pass
foobar.FooBar.dispatch_this.register(BarThing,y: f"Dispatching {y.__class__.__name__} from {x.__class__.__name__}")
# baz.py
import foo
import bar
import foobar
class Baz(foobar.FooBar):
pass
print(Baz().dispatch_this(foo.FooThing()))
print(Baz().dispatch_this(bar.BarThing()))
如果我们python baz.py
在导入过程中注册了调度员:
>>> Dispatching FooThing from Baz
>>> Dispatching BarThing from Baz
有没有办法解决这个问题?那就是在 child 类中“复制”调度程序方法(或注册表,我应该说)?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。