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

python operator overloading deal with type

def __add__(self, other):
    if isinstance(other, self.__class__):
        return self.x + other.x
    elif isinstance(other, int):
        return self.x + other
    else:
        raise TypeError("unsupported operand type(s) for +: '{}' and '{}'").format(self.__class__, type(other))
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 :: deploy django on nginx gunicorn 
Python :: add values from 2 columns to one pandas 
Python :: python global variable unboundlocalerror 
Python :: logger 
Python :: check if value is in series pandas 
Python :: convolution operation pytorch 
Python :: longest common prefix 
Python :: discord bot python get message id 
Python :: How to clone or copy a list in python 
Python :: giving number of letter in python 
Python :: split range python 
Python :: title() in python 
Python :: keys function in python 
Python :: python check if number contains digit 
Python :: python import function from file 
Python :: sqlalchemy function for default value for column 
Python :: when iterating through a pandas dataframe using index, is the index +1 able to be compared 
Python :: delete multiple dataframes at once in python 
Python :: object python 
Python :: Sort index values with pandas 
Python :: create an empty list in python 
Python :: python rounding numbers to n digits 
Python :: cast as float python 
Python :: variables in python 
Python :: possible substrings of a string python 
Python :: python using set 
Python :: django delete model from database 
Python :: turn numpy function into tensorflow 
Python :: floor function in python 
Python :: python string 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =