0

What is operator-overloading in python?

14th Apr 2018, 7:47 AM
Mohammad Yasir
Mohammad Yasir - avatar
3 Respostas
+ 10
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.
14th Apr 2018, 8:31 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
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.
14th Apr 2018, 8:00 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
please give me examples
14th Apr 2018, 8:23 AM
Mohammad Yasir
Mohammad Yasir - avatar