Abstract method with single-dispatch generic functions

Viewed 142

I want to put single-dispatch method in functools module with abstract method in abc module in one method in my abstract class and then using the method in my subclasses based on first argument type. but the problem is the dispatch method doesn't works.

class Abstract(metaclass=ABCMeta):
    ...
    ...
    ...

    @singledispatchmethod 
    @abstractmethod
    def apply(self, element:str):
        raise NotImplementedError

   @apply.register
   @abstractmethod
   def _(self, element: int):
       raise NotImplementedError


class A(Abstract):
    def apply(self, element: str):
        print(f'this is string -> {element}')

    def _(self, element: int):
        print(f'this is intiger -> {element}')


>>>instance = A()
>>>instance.apply(2)
#will return (this is string -> 2)

i have solved this problem in other way but i'm curious about this problem if it has an answer

0 Answers
Related