- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- 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 can we speed up Python "in" operator?
- How do we return multiple values in Python?
- How we can break a string with multiple delimiters in Python?
- How can we use Python Ternary Operator Without else?
- How do we use equivalence (“equality”) operator in Python classes?
- Can we change operator precedence in Python?
- How do we access class attributes using dot operator in Python?
- How can we print multiple blank lines in python?
- How we can extend multiple Python classes in inheritance?
- How we can split Python class into multiple files?
- How do I use method overloading in Python?
- How to use Operator Overloading in C#?
- How to implement operator overloading in C#?