coady / multimethod

Multiple argument dispatching.
https://coady.github.io/multimethod
Other
277 stars 24 forks source link

Is there a way to dispatch superclass methods? #74

Closed MordorianGuy closed 1 year ago

MordorianGuy commented 1 year ago

I would like to dispatch the __init__ method of a superclass. But it does not work with multimethod & multimeta in the way I expected. Moreover, manual registration is impossible as I cannot access the parent __init__ method inside the class body. I have described more here.

coady commented 1 year ago

To share a method across classes, the register decorator can be used. There's no direct equivalent of super, though it can be simulated by modifying or coercing the arguments.

class Base:
    @multimethod
    def __init__(self, arg: int):
        self.arg = arg

class Subclass(Base):
    @Base.__init__.register
    def __init__(self, arg: bool):
        super().__init__(int(arg))

assert Subclass(True).arg == 1