- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we do Python operator overloading with multiple operands?
You can do Python operator overloading with multiple operands just like you would do it for binary operators. For example, if you want to overload the + operator for a class, you'd do the following −
class Complex(object): def __init__(self, real, imag): self.real = real self.imag = imag def __add__(self, other): real = self.real + other.real imag = self.imag + other.imag return Complex(real, imag) def display(self): print(str(self.real) + " + " + str(self.imag) + "i") a = Complex(10, 5) b = Complex(5, 10) c = Complex(2, 2) d = a + b + c d.display()
This will give the output −
17 + 17i
- How will you explain Python Operator Overloading?
- How we can bundle multiple python modules?
- How do we return multiple values in Python?
- How we can break a string with multiple delimiters in Python?
- Can we change operator precedence in Python?
- How can we use Python Ternary Operator Without else?
- How do I use method overloading in Python?
- How we can extend multiple Python classes in inheritance?
- How we can split Python class into multiple files?
- How can we print multiple blank lines in python?
- How do we access class attributes using dot operator in Python?
- Overloading unary operator in C++?
- How can we speed up Python "in" operator?
- How to use Operator Overloading in C#?
- How to implement operator overloading in C#?