Why is it called operator overloading and not overriding in Python? -
if want change behavior of inherited method, this:
class a: def changeme(): print('called a') class b(a): def changeme(): print('called b') i believe example of overriding in python.
however, if want overload operator, similar:
class c: anumber = 0 def __add__(self, operand): print("words") return self.anumber + operand.anumber = c() b = c() a.anumber += 1 b.anumber += 2 print(a + b) # prints "words\n3" i thought maybe operator methods overridden in python since overload using optional parameters , call operator overloading out of convention.
but couldn't override, since '__add__' in object.__dict__.keys() false; method needs member of parent class in order overridden (and classes inherit object when created).
where gap in understanding?
overloading means 2 methods same name , different signatures + return types. overriding means 2 methods same name, wherein sub method has different functionality.the main difference between overloading , overriding in overloading can use same function name different parameters multiple times different tasks on class. , overriding means can use same name function name same parameters of base class in derived class. called re usability of code in program.
Comments
Post a Comment