Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complex-numbers: operator overloading #724

Closed
SaschaMann opened this issue Oct 5, 2017 · 5 comments
Closed

complex-numbers: operator overloading #724

SaschaMann opened this issue Oct 5, 2017 · 5 comments

Comments

@SaschaMann
Copy link
Contributor

SaschaMann commented Oct 5, 2017

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?

@ilya-khadykin
Copy link
Contributor

ilya-khadykin commented 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

@cmccandless
Copy link
Contributor

__abs__() can also be overridden.

@susg
Copy link
Contributor

susg commented Oct 24, 2017

Hi, how do I override abs and conjugate? And in order to override exp it should renamed to __pow__ and div to __truediv__ if using python 3.3.

@cmccandless
Copy link
Contributor

@susg Hi! If you are interested in working on this, please create a pull request to claim the issue.

There is no operator to override for conjugate, so that should remain a traditional method.

exp is actually different from __pow__. See Wolfram Alpha's explanation of Complex Exponentiation for more information.

For more info on overloading operators, see below:

@m-a-ge susg raised a good point; in Python 2, the operator for / is __div__, while in Python 3 it is __truediv__. Perhaps the template solution should contain both, with a note in the differences between the two? Having both will still interpret in either 2 or 3, so this would simply be covering all bases.

susg added a commit to susg/python that referenced this issue Oct 24, 2017
susg added a commit to susg/python that referenced this issue Oct 24, 2017
Replace add, sub, mul, div, with __add__, __sub__, __mul__,
__truediv__ to showcase use of Pythons's magic methods on
operator overloading. Added __eq__ in example.py to compare
two ComplexNumber objects.

Closes exercism#724
@cmccandless
Copy link
Contributor

As this is no longer pending resolution of #1057, I am removing the pinned label.

@cmccandless cmccandless removed the pinned label Nov 7, 2017
susg added a commit to susg/python that referenced this issue Nov 9, 2017
Replace add, sub, mul, div, with __add__, __sub__, __mul__,
__truediv__ to showcase use of Pythons's magic methods on
operator overloading.
Add __eq__ in example.py to compare two
ComplexNumber objects.

Closes exercism#724
susg added a commit to susg/python that referenced this issue Nov 9, 2017
Replace add, sub, mul, div, with __add__, __sub__, __mul__,
__truediv__ to showcase use of Pythons's magic methods on
operator overloading.
Add __eq__ in example.py to compare two
ComplexNumber objects.

Closes exercism#724
cmccandless pushed a commit that referenced this issue Jan 9, 2018
* complex-numbers: operator overloading

Replace add, sub, mul, div, with __add__, __sub__, __mul__,
__truediv__ to showcase use of Pythons's magic methods on
operator overloading.
Add __eq__ in example.py to compare two
ComplexNumber objects.

Closes #724

* override abs with __abs__

* add hints.md and regenerate README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants