[issue5211] Fix complex type to avoid coercion in 2.7.

Blair report at bugs.python.org
Fri Nov 26 04:45:06 CET 2010


Blair <bidihall at gmail.com> added the comment:

Hi Mark,

I thought that this had all been fixed, but it seems not.

Consider the following:

class xcomplex( complex ):
    def __new__(cls,*args,**kwargs):
        return complex.__new__(cls,*args,**kwargs)
    def __add__(self,x):
        return xcomplex( complex.__add__(self,x) )
    def __radd__(self,x):
        print "larg: ", type(x),"returning: ",
        return xcomplex( complex.__radd__(self,x) )

class xfloat(float):
    def __new__(cls,*args,**kwargs):
        return float.__new__(cls,*args,**kwargs)
    def __add__(self,x):
        return xfloat( float.__add__(self,x) )
    def __radd__(self,x):
        print "larg: ", type(x),"returning: ",
        return xfloat( float.__radd__(self,x) )

z = 1j
xz = xcomplex(1j)
f = 1.0
xf = xfloat(1.0)

print
print "-----------"
print "expect xcomplex:", type(z + xz)
print "expect xcomplex:", type(f + xz)
print "expect xfloat:", type(f + xf)
print "expect ???:", type(z + xf)

When this runs, the first three conversions are fine, the last is not: there
is no call to xfloat.__radd__. It seems that the builtin complex type simply
thinks it is dealing with a float. Here is the output

-----------
expect xcomplex: larg:  <type 'complex'> returning:  <class
'__main__.xcomplex'>
expect xcomplex: larg:  <type 'float'> returning:  <class
'__main__.xcomplex'>
expect xfloat: larg:  <type 'float'> returning:  <class '__main__.xfloat'>
expect ???: <type 'complex'>

The last line shows that no call to __radd__ occurred.

Is there anything that can be done now about this now, or is it just too
late?

Regards

Blair

At 01:13 a.m. 31/05/2010, you wrote:

Mark Dickinson <dickinsm at gmail.com> added the comment:

r78280 didn't remove the implicit coercion for rich comparisons;  that's now
been done in r81606.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5211>
_______________________________________Python tracker <
report at bugs.python.org>

----------
Added file: http://bugs.python.org/file19820/unnamed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5211>
_______________________________________
-------------- next part --------------
Hi Mark,<br><br>
I thought that this had all been fixed, but it seems not.<br><br>
Consider the following:<br><br>
<font face="Courier, Courier">class xcomplex( complex ):<br>
    def __new__(cls,*args,**kwargs):<br>
        return complex.__new__(cls,*args,**kwargs)<br>
    def __add__(self,x):<br>
        return xcomplex( complex.__add__(self,x) )<br>
    def __radd__(self,x):<br>
        print &quot;larg: &quot;, type(x),&quot;returning: &quot;,<br>
        return xcomplex( complex.__radd__(self,x) )<br><br>
<br>
class xfloat(float):<br>
    def __new__(cls,*args,**kwargs):<br>
        return float.__new__(cls,*args,**kwargs)<br>
    def __add__(self,x):<br>
        return xfloat( float.__add__(self,x) )<br>
    def __radd__(self,x):<br>
        print &quot;larg: &quot;, type(x),&quot;returning: &quot;,<br>
        return xfloat( float.__radd__(self,x) )<br><br>
<br>
z = 1j<br>
xz = xcomplex(1j)<br>
f = 1.0<br>
xf = xfloat(1.0)<br><br>
<br>
print<br>
print &quot;-----------&quot;<br>
print &quot;expect xcomplex:&quot;, type(z + xz) <br>
print &quot;expect xcomplex:&quot;, type(f + xz) <br>
print &quot;expect xfloat:&quot;, type(f + xf)<br>
print &quot;expect ???:&quot;, type(z + xf)<br><br>
</font>When this runs, the first three conversions are fine, the last is not: there is no call to xfloat.__radd__. It seems that the builtin complex type simply thinks it is dealing with a float. Here is the output<br><br>

<font face="Courier, Courier">-----------<br>
expect xcomplex: larg:  &lt;type &#39;complex&#39;&gt; returning:  &lt;class &#39;__main__.xcomplex&#39;&gt;<br>
expect xcomplex: larg:  &lt;type &#39;float&#39;&gt; returning:  &lt;class &#39;__main__.xcomplex&#39;&gt;<br>
expect xfloat: larg:  &lt;type &#39;float&#39;&gt; returning:  &lt;class &#39;__main__.xfloat&#39;&gt;<br>
expect ???: &lt;type &#39;complex&#39;&gt;<br><br>
</font>The last line shows that no call to __radd__ occurred. <br><br>
Is there anything that can be done now about this now, or is it just too late?<br><br>
Regards<br><br>Blair<br><br>
At 01:13 a.m. 31/05/2010, you wrote:<br><br>
<blockquote type="cite" class="cite" cite="">Mark Dickinson &lt;<a href="mailto:dickinsm at gmail.com">dickinsm at gmail.com</a>&gt; added the comment:<br><br>
r78280 didn&#39;t remove the implicit coercion for rich comparisons;  that&#39;s now been done in r81606.<br><br>
----------<br><br>
_______________________________________<br>
Python tracker &lt;<a href="mailto:report at bugs.python.org">report at bugs.python.org</a>&gt;<br>
&lt;<a href="http://bugs.python.org/issue5211">http://bugs.python.org/issue5211</a>&gt;<br>
_______________________________________Python tracker &lt;<a href="mailto:report at bugs.python.org">report at bugs.python.org</a>&gt;</blockquote>


More information about the Python-bugs-list mailing list