Tutorial Python 3.3: Operator Overloading - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Python 3.3: Operator Overloading (/showthread.php?tid=7940) |
Python 3.3: Operator Overloading - Covid-19 - 05-24-2022 Operator Overloading: Hi guys, I hope You all are doing well. In today's thread we will discuss operator overloading. But before discussing that we need to discuss a small topic which is Magic Method. These are special methods. We used one of these methods before in our thread which was __init__(). It starts and ends with double underscores and is also known as Dunders. Let’s code it for better understanding. Code: class Student: Here we have used three Magic methods: the first one is __init__() to initialize an instant for a class and the second one str() is used for creating output for end users while repr() is mainly used for debugging and development. repr's goal is to be unambiguous and str's is to be readable. If your program has both repr() and str() methods the repr will not be called automatically the str function will call instead. The other most common functionality of magic methods is operator overloading. Operator overloading means making operators functional for different types of classes. Like if we created two objects of one class and we want to use addition between them so we will first define that operation for them as it is not defined built in. It is only defined for integers and strings (Concatenation). But not for classes that you made so if we want to make it functional we will use operator overloading. Let’s code a program for this.. Code: class Student: So guys as you can see we have used another magic operator in our program and we used this to overload + operator and this function will now return us addition of marks of two Students. To do task : Check out other Mapping operators to Function and try to make separate programs for each. Quote:Other Magic method for common Operators: Code: ########## Program to demonstrate Truediv method ############# As you can see In this program we have overloaded the method in our own way. It does not return division of numbers but it returns text with some formats. So that’s it for now. Most importantly, Don’t Forget to practice all the other magic methods. "Keep Good care of your Health Allah Hafiz and Good Bye Till the next post" RE: Python 3.3: Operator Overloading - Mr.Kurd - 05-25-2022 Again nice topic to discuss. Thanks.. Title was new for me but looks like we don't have this in Java as well RE: Python 3.3: Operator Overloading - Covid-19 - 05-26-2022 (05-25-2022, 06:20 PM)Mr.Kurd Wrote: Again nice topic to discuss. Thanks.. Thank you Man your comments always inspires me |