Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 3.3: Operator Overloading

2 Replies, 2801 Views

[Image: BFMMlbcQvml9HSqXcvNp]

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:
    def __init__(self, name, age ,marks, roll_number):
        self.name = name
        self.age = age
        self.marks = marks
        self.roll_number = roll_number
    def __str__(self):
        return "Marks of " + self.name + " is " + str(self.marks)
    def __repr__(self):
        return "Full name is " + self.name

s1 = Student("Alien", 31, 65, 4260)
s2 = Student("Covid", 34, 43, 2135)
s3 = Student("Kurd", 21, 54, 2112)
print(s1.__repr__())
print(s2.__str__())
print(s3.__repr__())


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:
    def __init__(self, name, age ,marks, roll_number):
        self.name = name
        self.age = age
        self.marks = marks
        self.roll_number = roll_number
    def __str__(self):
        return "Marks of " + self.name + " is " + str(self.marks)
    def __repr__(self):
        return "Full name is " + self.name

    def __add__(self, other):
        return self.marks + other.marks
s2 = Student("Covid", 34, 43, 2135)
s3 = Student("Kurd", 21, 54, 2112)

print(s1 + s2)

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.

Exclamation  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:

__sub__ for -
__mul__ for *
__truediv__ for /
__floordiv__ for //
__mod__ for %
__pow__ for **
__and__ for &
__xor__ for ^
__or__ for |


Code:
########## Program to demonstrate Truediv method  #############
class Some_text:
    def __init__(self, text):
        self.text = text
    def __truediv__(self, other):
        line = "-" * len(other.text)
        return self.text + '\n' + line  + '\n' + other.text

txt = Some_text("Hi, There What is your name")
txt1 = Some_text("I am Covid")

print(txt / txt1)


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"
Love me like you do Heart
Again nice topic to discuss. Thanks..

Title was new for me but looks like we don't have this in Java as well
Rs
* Thankful to Allah *
Kurdy
(05-25-2022, 06:20 PM)Mr.Kurd Wrote: Again nice topic to discuss. Thanks..

Title was new for me but looks like we don't have this in Java as well

Thank you Man your comments always inspires me
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,646 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,826 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,775 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,804 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,806 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.1: OOP Covid-19 2 1,629 05-24-2022, 06:37 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)