logoalt Hacker News

Overloaded Overloading

31 pointsby redsymbollast Wednesday at 11:44 AM8 commentsview on HN

Comments

reichsteintoday at 11:13 AM

So the `+` is not _overloaded_, the `+` syntax is a shorthand for calling a method named `__add__` on the value of the first operand, with the value of the second operand as an argument . That is: `e1 + e2` is syntactic sugar for `e1.__add__(e2)`, no more and no less.

It's not "operator overloading" any more than two _different_ classes having a `length` property is "length overloading".

Using the term "operator overloading" to begin with is the error here.

show 2 replies
faresahmedtoday at 2:48 PM

Another comment already explains that this is regular method overriding in Python. Surely, some languages call user-defined operators 'Operator overloading' (e.g. C#) but the mechanism which Python uses (an `object` base class) IS overriding.

As to overloading as in multiple signatures (No types here!) for the same method name, it is not possible in the regular sense in Python since an object is more or less a dictionary of names. However, the dynamicity of the language will happily allow you to simulate it with YOUR own rules for disambiguation if you like. In practice, you'd 1. Don't 2. *args, **kwargs, Branches of is None and isinstance, etc.

rf15today at 9:48 AM

Good introduction to the space in Python (although a lot of languages have it, and just as many disallow it). I'm still on the fence regarding how useful or confusing it is. without (or even with) good variable names, "a + b" is an absolute mystery, especially when handling objects that come from classes you have no detailed internal understanding of.

show 2 replies
hnd9q09qk4today at 10:20 AM

[flagged]

show 1 reply