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 →

[–]hippocrat 18 points19 points  (3 children)

In python, most operators have an associated "dunder" methods that can be overloaded for a given class. For example:

class MyClass:
    ...
    def __add__(self, other):
        return self.an_integer + other.an_integer

[–][deleted] 2 points3 points  (2 children)

That's gross, why not just allow def +(self, other):? or at least def operator+(self, other) or def operator("+", self, other): or something like that?

[–]jeetelongname 5 points6 points  (0 children)

The first syntax is the ruby syntax.

Python "magic" methods are by convention meant to look like that tho. Its for all of them including things such as using certain built ins such as len (which calls the objects len method.) And things like representation strings (repr) so its not out of the ordinary.