What is operator-overloading in python? | SoloLearn: Learn to code for FREE!


What is operator-overloading in python?

4/14/2018 7:47:22 AM

Mohammad Yasir

3 Answers

New Answer


For integers: 2 + 4 = 6 For strings: "2" + "4" = "24" For tuples: (2,) + (4,) = (2, 4) and so on. Each time you use the same operator - "+" - and it knows what to do with every data type. This is because it is implemented differently for each class. And that is what operator overloading is about.


It is a property of the operator to act differently depending on the class of its arguments. You can use the __add__ operator (expressed by the 'plus' sign) with integers, floats, strings, lists, tuples and other data types. Each time it will act differently - performing mathematical operation or concatenation or joining/merging - its behavior is class-dependent.


please give me examples