5

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()
Anchith Acharya
  • 369
  • 1
  • 4
  • 16
  • 1
    Please did you get a solution to your problem so that you can share with us – Anas Jun 16 '22 at 11:13
  • 2
    @Anas 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()` – Anchith Acharya Jun 17 '22 at 12:34
  • 1
    thanks for your comments, I used the dispatch in the 'plum' package without arguments and it worked – Anas Jun 28 '22 at 22:15

0 Answers0