Код: Выделить всё
from typing import Callable
class MyClass:
pass
def dynamic_method(self, x: int, y: str) -> str:
return f"{x} - {y}"
setattr(MyClass, 'dynamic_method', dynamic_method)
instance = MyClass()
print(instance.dynamic_method(42, "hello")) # This works
from inspect import signature
print(signature(MyClass.dynamic_method))
Подробнее здесь: https://stackoverflow.com/questions/792 ... preserving
Мобильная версия