Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

overload operator python

class Vector:
  def __init__(self, x, y):
    self.x = x
    self.y = y
   
   def __add__(self, other):
      return Vector(self.x + other.x, self.y + other.y)
Comment

Python Overloading the + Operator

class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __str__(self):
        return "({0},{1})".format(self.x, self.y)

    def __add__(self, other):
        x = self.x + other.x
        y = self.y + other.y
        return Point(x, y)
Comment

operator overloading python

# Operator overloading
# Overload + and += operators
class Complex_number:
    def __init__(self, real, imaginary):
        self.real = real
        self.imaginary = imaginary

    def __add__(self, right):       # binary operators must provide 2 parameters
        return Complex_number(self.real + right.real, 
                       self.imaginary + right.imaginary)

    def __iadd__(self, right):
        """Overrides the += operator."""
        self.real += right.real
        self.imaginary += right.imaginary
        return self

    def __repr__(self):
        return (f'({self.real}' + 
                (' + ' if self.imaginary >= 0 else ' - ') +
                f'{abs(self.imaginary)}i)')

x = Complex_number(real = 2, imaginary = 4)
x
# (2 + 4i)
y = Complex_number(real = 5, imaginary = -1)
y
# (5 - 1i)
x + y
# (7 + 3i)
x += y
x
# (7 + 3i)
y
# (5 - 1i)
Comment

Operator Overloading in Python

# Python program to show use of
# + operator for different purposes.
 
print(1 + 2)
 
# concatenate two strings
print("Geeks"+"For")
 
# Product two numbers
print(3 * 4)
 
# Repeat the String
print("Geeks"*4)
Comment

PREVIOUS NEXT
Code Example
Python :: list -1 python 
Python :: django annotate vs aggregate 
Python :: how to get all messages from a telegram group with telethon 
Python :: markers seaborn 
Python :: python convert input into lowercase 
Python :: convert list to set python 
Python :: xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 0 
Python :: extract DATE from pandas 
Python :: fibonacci series using dynamic programmig approach 
Python :: cv2.copyMakeBorder 
Python :: how to split a string with newline in python 
Python :: python package for confluence 
Python :: dot operator in python 
Python :: bin to int python 
Python :: initialize a 2d list python 
Python :: transpose list 
Python :: correlation for specific columns 
Python :: convert a string into a list in Python 
Python :: pytest teardown method 
Python :: How to check palindrom in python 
Python :: selenium webdriver options python 
Python :: django serve media folder 
Python :: append two list of number to one python 
Python :: convert string to lowercase in python 
Python :: create button in pyqt 
Python :: python i++ 
Python :: render() in django 
Python :: Insurance codechef solution 
Python :: not equal python 
Python :: size array python 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =