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 :: pandas assign multiple columns 
Python :: awesome python 
Python :: Send Variable Over In Python Views 
Python :: Find From Table Django 
Python :: Reading Excel and other Microsoft Office files 
Python :: self argument in python 
Python :: aws ses service python example 
Python :: reduce size of list 
Python :: enumerate for string 
Python :: Boolean comparison w/out if statements 
Python :: how to append the items in list 
Python :: how to reassign a key py 
Python :: Using iterable unpacking operator * 
Python :: how to filter even or odd model id in django 
Python :: convert python code to dart online 
Python :: spark sparsevector to list 
Python :: django rotatingfilehandler 
Python :: python bill 
Python :: sort files in windows order python 
Python :: not staments python 
Python :: how to delete blank rows from text file in spyder 
Python :: how to place an id to every element in list in python 
Python :: python matplotlib fullscreen zoom 
Python :: Improve the Request Use Proxies 
Python :: Python Old style formatting 
Python :: pd drop a range of dates 
Python :: how to make a timer in pyothn 
Python :: how to change the title of the top bar in python 
Python :: numpy topk 
Python :: the most effective search algorithm in python 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =