How do I make multipledispatch accept objects of same class?
Asked Answered
M

0

6

I am using multipledispatch to make a Point class, which has three constructors: one that takes a single integer, one that takes two, and one that takes an object of type Point. But I am unable to implement the third constructor as I don't know what argument to give to the @dispatch decorator, since the class Point is not yet defined. I have currently resorted to using object, but is there any way I can use Point itself?

Here's (part of) my code:

from multipledispatch import dispatch

class Point:
    @dispatch(int,int)
    def __init__(self, y = None,x = None):
        self.y = y
        self.x = x

    @dispatch(int)
    def __init__(self, yx = None):
        self.__init__(yx,yx)

    @dispatch(object)    # is there any way I can use @dispatch(Point)?
    def __init__(self, p: "Point") -> "Point":    # using forward reference
        self = p.copy()
Machmeter answered 4/11, 2020 at 8:53 Comment(3)
Please did you get a solution to your problem so that you can share with usSweptback
@Sweptback I don't remember now, but perhaps you could do it like this: @dispatch(object) and in the init method, if isinstance(p, Point): self = p.copy()Machmeter
thanks for your comments, I used the dispatch in the 'plum' package without arguments and it workedSweptback

© 2022 - 2024 — McMap. All rights reserved.