Would be very easy to solve had python been a static programming language that supported overloading. I am making a class called Complex which is a representation of complex numbers (I know python has its own, but i want to make one myself), where a is the real number and b is the imaginary (Complex(a, b)
). It should support adding Complex instances together (Complex(2, 4) + Complex(4, 5) = Complex(6, 9)
), as well as adding an integer (Complex(2, 3) + 4 = Complex(6, 3)
). However, due to the nature of python...
__add__(self, other):
...I have to choose which the class will support, because it won't recognize types at compile-time, as well as not supporting overloading of functions. What is the best solution? Do I have to write an if statement in relation to the datatype of the other
parameter?