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
surprise overriding __radd__ in subclass of complex #36275
Comments
I had presented this on tutor and python-list: class Complex(complex):
def __mul__(self,other):
other=Complex(other)
t = complex.__mul__(self,other)
return Complex(t.real,t.imag)
__rmul__ = __mul__
def __add__(self,other):
other=Complex(other)
return Complex(self.real.__add__
(other.real),self.imag.__add__(other.imag))
__radd__ = __add__ Then: print type(Complex(5,4) * 7)
But: print type(7 + Complex(5,4))
Danny Yoo, after looking into it pretty deeply - "In any case, this is definitely a bug in the |
Logged In: YES You're right, something's weird. I'll add this to my list. |
Logged In: YES It seems that * is the exception -- all other operators (+,
The weirdness is in int_mul. If the right argument is not an So 7 + Complex() takes the path through int_mul that calls But wait...! There's another mystery. Ints don't know how to Solution: add the following to your Complex class: def __coerce__(self, other):
x = complex.__coerce__(self, other)
if x is NotImplemented:
return x
a, b = x
return Complex(a), Complex(b) I don't think I want to do anything to fix this, although I In the long run, I think complex should stop using coercion Also, in the long run, the sq_repeat and sq_concat slots |
Logged In: YES Guido - Thanks for your attention to my report. Another bit of suprise with operator overriding from __future__ import division
But: everything the same *without* calling print type(a/b)
Based your analysis of the previous My general sense, though, is that this issue Don't think anyone will expect a change in Obviously not a big issue if the other changes Art |
Logged In: YES Have just re-read my submission - and The gist, I am sure you will see, is that And I am assuming that behavior is unanticipated. And I assuming there is no reason to submit this a Art |
Logged In: YES If you want to override division in a complex subclass, you |
Logged In: YES Knew I'd embarass myself soon enough in this Found the __truediv__ answer on the train into Too late. Thanks again - oops for the false alarm. Art |
Logged In: YES Guido, Fred, is there anything to be done or should this be |
Logged In: YES This is fixed in 2.3. Can't remember if it was fixed in 2.2 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: