Answers for "demonstrate the working of operator overloading in python"

0

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))
Posted by: Guest on December-09-2020
0

python override add

class MyClass:
  def __add__(self, b):
    return 5 + b
Posted by: Guest on September-22-2020

Code answers related to "demonstrate the working of operator overloading in python"

Python Answers by Framework

Browse Popular Code Answers by Language