2

(この回答multipledispatchに基づいて)を使用して、デフォルトのパラメーターで関数をオーバーロードしています

from multipledispatch import dispatch

class TestExampleTest(AbstractTest):
    @dispatch(ClassOne, bool, bool)
    def function(self, my_class, a=True, b=True):
        do_something()

    @dispatch(ClassTwo, bool)
    def function(self, my_class, a=True):
        do_something_else()

アイテムfunction()に値を渡さずに呼び出しているときbool

self.function(ClassOne())

私は得る

NotImplementedError: 関数の署名が見つかりませんでした

完全なスタック トレース:

ExampleTest.py:27 (TestExampleTest.test_example_test)
self = <ExampleTest.TestExampleTest object at 0x04326BB0>

    def test_example_test(self):
>       self.function(ClassOne())

ExampleTest.py:29: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <dispatched function>
args = (<ExampleTest.ClassOne object at 0x043262B0>,), kwargs = {}
types = (<class 'ExampleTest.ClassOne'>,), func = None

    def __call__(self, *args, **kwargs):
        types = tuple([type(arg) for arg in args])
        func = self.dispatch(*types)
        if not func:
            raise NotImplementedError('Could not find signature for %s: <%s>' %
>                                     (self.name, str_signature(types)))
E           NotImplementedError: Could not find signature for function: <ClassOne>

..\..\..\..\Automation\lib\site-packages\multipledispatch\dispatcher.py:434: NotImplementedError

注:私は@dispatchすべて一緒にドロップして、次のようなことができることを知っています

def function(self, my_class_one=None, my_class_two=None, a=True, b=True):
    if my_class_one:
        do_something()
    elif my_class_two:
        do_something_else()

ただ、今の体制を維持できるかは疑問です。

どうすれば修正できますか?

4

1 に答える 1