question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

complex-numbers: operator overloading

See original GitHub issue

The complex-number exercise testsuite and example use methods add, mul etc. instead of the operators __add__, __mul__. Using the latter, one could do ComplexNumber(1, 2) + ComplexNumber(3, 4) instead of ComplexNumber(1, 2).add(ComplexNumber(3, 4)) which might be a more natural/pythonic way to write these. Perhaps changing it to the operator version would make this exercise a bit more interesting and applicable to “real life” use cases?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cmccandlesscommented, Oct 6, 2017

__abs__() can also be overridden.

1reaction
ilya-khadykincommented, Oct 6, 2017

@SaschaMann, good point, thanks! I totally agreed.

It* will showcase use of Python`s magic methods an operator overloading (Java for example does not have such luxury) in action. Perhaps it would be a good idea to include a hint explaining what it is in the exercise as well

Read more comments on GitHub >

github_iconTop Results From Across the Web

C++ Program to Subtract Complex Number Using Operator ...
Example: Binary Operator Overloading to Subtract Complex Number ... In this program, three objects of type Complex are created and user is asked...
Read more >
Operator overloading Complex numbers
a basically arithmetic type for which operator overloading makes sense. – complex added as explicit type in 1999 C standard.
Read more >
Operator Overloading in C++ - GeeksforGeeks
So the main idea behind “Operator overloading” is to use c++ operators with class variables or class objects. Redefining the meaning of ...
Read more >
Add/Subtract Complex Numbers using Operator Overloading ...
If we define complex numbers as objects, we can easily use arithmetic operators such as additional (+) and subtraction (-) on complex numbers...
Read more >
C++ program to overload addition operator to add two ...
Overload the + operator and take another complex number c2 as argument · define a complex number called ret whose real and imag...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found