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 :: pandas count empty string values 
Python :: numpy.sign() in Python 
Python :: django example 
Python :: keras conv2d batchnorm 
Python :: keyword python 
Python :: Python NumPy broadcast_arrays() Function Example 
Python :: create new python environment check 
Python :: oserror: invalid cross-device link 
Python :: PY | websocket - server 
Python :: python @property 
Python :: training linear model sklearn 
Python :: split string into groups of 3 chars python 
Python :: how to add coloumn based on other column 
Python :: how to copy content of one file to another in python 
Python :: gzip folder python 
Python :: python image crop 
Python :: py -m pip 
Python :: Simple Splash screen in pyqt5 
Python :: write list to csv python 
Python :: how to get the max of a list in python 
Python :: geopandas geometry length 
Python :: np.r_ 
Python :: how to make python into exe 
Python :: drop row with duplicate value 
Python :: python enumerate for loop 
Python :: maximum element in dataframe row 
Python :: set python 3 as default mac 
Python :: python how to play mp3 file 
Python :: Converting Dataframe from the multi-dimensional list with column name 
Python :: python datetime move forward one day 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =