7

If I want to change the behavior of an inherited method, I would do something like this:

class a:
    def changeMe(self):
        print('called a')

class b(a):
    def changeMe(self):
        print('called b')

I believe this is an example of overriding in Python.

However, if I want to overload an operator, I do something very similar:

class c:
    aNumber = 0
    def __add__(self, operand):
        print("words")
        return self.aNumber + operand.aNumber

a = c()
b = c()
a.aNumber += 1
b.aNumber += 2
print(a + b) # prints "words\n3"

I thought that maybe the operator methods are really overridden in python since we overload using optional parameters and we just call it operator overloading out of convention.

But it also couldn't be an override, since '__add__' in object.__dict__.keys() is False; a method needs to be a member of the parent class in order to be overridden (and all classes inherit from object when created).

Where is the gap in my understanding?

michen00
  • 764
  • 1
  • 8
  • 32
  • 1
    I believe overriding is referred to typically taking a method that already exists and overriding it for use with your specific implementation. Operator overloading is typically taking operators within the language, i.e. '+', and defining them for objects that normally do not have that built in operation. For example, if I make a class called 'item' that has a price and a desc., I could use operator overloading to define what item1 + item2 does, which could say... return the combined price. – Easton Bornemeier Jul 28 '17 at 15:37
  • 1
    Please use self in changeme function signature. class a: def changeme(self): print('called a') class b(a): def changeme(self): print('called b') – Ashish Bainade May 19 '19 at 11:36

2 Answers2

4

I guess since the original question specifically asked about the gap in my own understanding, I am best-positioned to answer it. Go figure.

What I failed to understand was that whereas overriding depends on inheritance, overloading does not. Rather, Python matches methods for overloading based on name only.

For a subclass to override a method, the method does indeed need to exist in the parent class. Therefore, the def __add__ portion is not an example of overriding.

(In this case, I also did not fully understand that if the interpreter sees a + operator, it will look to the class of the operands for a definition of the __add__ magic method.)

Because the + operator is essentially an alias for __add__(), the same name is being used. Operator overloading is in fact an example of overloading because we are changing the behavior of the name (+ or __add__) when it is called with novel parameters (in my example, objects of class c).

michen00
  • 764
  • 1
  • 8
  • 32
2

Overloading means 2 methods with the SAME Name and different signatures + return types. Overriding means 2 methods with the SAME name, wherein the sub method has different functionality.The main difference between overloading and overriding is that in overloading we can use same function name with different parameters for multiple times for different tasks with on a class. and overriding means we can use same name function name with same parameters of the base class in the derived class. this is also called as re usability of code in the program.

Dragon
  • 1,194
  • 17
  • 24
  • 1
    I agree with everything you wrote except the last sentence: code re-use has nothing to do with either overloading or overriding. Code *may* be re-used when doing any of the two but you can also implement any of them without having any code re-used. – Nir Alfasi Jul 28 '17 at 15:45
  • Yes Boss Totally agree with you. Think a little amendment is needed on that front – Dragon Jul 28 '17 at 15:51