Точнее, пока я использую функции это прекрасно работает
Код: Выделить всё
@multimethod
def disp(a, b):
print("dunno")
@multimethod
def disp(a: int, b: None):
print("int x none")
@multimethod
def disp(a: str, b: None):
print("str x none")
@multimethod
def disp(a: int, b: str):
print("int x str")
@multimethod
def disp(a: str, b: str):
print("str x str")
disp(None, None) # dunno
disp(1, None) # int x none
disp("test", None) # str x none
disp(1, "test") # int x str
disp("test", "test") # str x str
Код: Выделить всё
@dataclass
class Test:
x: int|str
y: int|str|None = None
def do(self):
self._dispatch(a = self.x, b = self.y)
@multimethod
def _dispatch(self, a, b): # default case
print("dunno")
@multimethod
def _dispatch(self, a: int, b: None):
print("int x none")
@multimethod
def _dispatch(self, a: str, b: None):
print("str x none")
@multimethod
def _dispatch(self, a: int, b: str):
print("int x str")
@multimethod
def _dispatch(self, a: str, b: str):
print("str x str")
# etc..
## what is printed is always the default case
t1 = Test(1)
type(t1.x)
t1.y is None
t1.do() # should be int x none
Test("test").do() # should be str x none
Test(1, "test").do() # should be int x str
Test("test", "test").do() # should be str x str
Подробнее здесь: https://stackoverflow.com/questions/790 ... ultimethod