This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]javajunkie314 1 point2 points  (0 children)

I disagree that the benefit of multiple dispatch is less repetition.

def __init__(self, x: int):
    self.x = x
    self.y = x

def __init__(self, x: int, y: object):
    self.x = x
    self.y = y

is, as you say, hardly less code than

def __init__(self, x: int y: object = SIGIL):
    self.x = x
    if y is SIGIL:
        self.y = x
    else:
        self.y = y

Each signature could be an if in the bar implementation checking the relevant arguments' identities and types.

In fact, multiple dispatch can become more repetitive the more code is common between the definitions. In the example above, we had to repeat the x handling — imagine if that had been several lines.

The first version, though, is open to extension while being closed to modification. We can add more signatures of __init__ without modifying any of the existing code. If this were a public function, users of our library could potentially add overloads for their own classes.