引用纯虚方法

Referring to a pure virtual method

我可能遗漏了一些明显的东西。我有一个简单的 class 层次结构,大致如下所示:

class Any:
    def op2_additive_add(self, other: 'Any') -> 'Any':
        raise NotImplementedError

    def op2_multiplicative_multiply(self, other: 'Any') -> 'Any':
        raise NotImplementedError

    def op2_exponential_power(self, other: 'Any') -> 'Any':
        raise NotImplementedError

    # A dozen of similar methods not shown

class Rational(Any):
    def op2_additive_add(self, other: 'Any') -> 'Rational':
        pass    # Implementation not shown

    def op2_multiplicative_multiply(self, other: 'Any') -> 'Rational':
        pass    # Implementation not shown

    def op2_exponential_power(self, other: 'Any') -> 'Rational':
        pass    # Implementation not shown

class Set(Any):
    def op2_additive_add(self, other: 'Any') -> 'Set':
        pass    # Implementation not shown

    def op2_multiplicative_multiply(self, other: 'Any') -> 'Set':
        pass    # Implementation not shown

    def op2_exponential_power(self, other: 'Any') -> 'Set':
        pass    # Implementation not shown

# Half a dozen of similar derived types not shown.

我正在实现一个调度程序 class,它应该在不知道它们的类型的情况下选择对两个操作数的请求操作,并将对正确方法的引用传递给执行程序。大致是这样的(下面的代码不行):

def select_operator(selector) -> typing.Callable[[Any, Any], Any]:
    if selector.such_and_such():
        return Any.op2_additive_add
    elif selector.so_and_so():
        return Any.op2_exponential_power
    # And so on

以上代码将无法运行,因为尝试调用返回的未绑定方法将绕过动态调度;即 select_operator(selector)(foo, bar) 将始终抛出 NotImplementedError.

到目前为止我能想到的最好的解决方案大致是这样的,它并不漂亮:

def select_operator(selector) -> str:
    if selector.such_and_such():
        return Any.op2_additive_add.__name__
    elif selector.so_and_so():
        return Any.op2_exponential_power.__name__
    # And so on

method_name = select_operator(selector)
getattr(foo, method_name)(bar)

TL;DR:如何将动态调度过程延迟到引用方法之后?

也许这样更合适?

class Any:
    def select_and_do_op(self, selector, other):
        if selector.such_and_such():
            return self.op2_additive_add.(other)
        elif selector.so_and_so():
            return self.op2_exponential_power(other)
    ...

foo.select_and_do_op(selector, bar)

更新

另一个解决方案:

def select_operator(selector):
    if selector.such_and_such():
        return lambda foo, bar: foo.op2_additive_add(bar)
    elif selector.so_and_so():
        return lambda foo, bar: foo.op2_exponential_power(bar)
    ...

operator = select_operator(selector)
result = operator(foo, bar)