子类上的 singledispatchmethod 的解决方法

问题描述

The new functools.singledispatchmethod (issue32380) class interacts poorly with subclasses. There is no way for a sub-class to override or extend the dispatch registry.

那当然是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 (将#修改为@)