[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 "larg: ", type(x),"returning: ",<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 "larg: ", type(x),"returning: ",<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 "-----------"<br>
print "expect xcomplex:", type(z + xz) <br>
print "expect xcomplex:", type(f + xz) <br>
print "expect xfloat:", type(f + xf)<br>
print "expect ???:", 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:Â <type 'complex'> returning:Â <class '__main__.xcomplex'><br>
expect xcomplex: larg:Â <type 'float'> returning:Â <class '__main__.xcomplex'><br>
expect xfloat: larg:Â <type 'float'> returning:Â <class '__main__.xfloat'><br>
expect ???: <type 'complex'><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 <<a href="mailto:dickinsm at gmail.com">dickinsm at gmail.com</a>> added the comment:<br><br>
r78280 didn't remove the implicit coercion for rich comparisons;Â that's now been done in r81606.<br><br>
----------<br><br>
_______________________________________<br>
Python tracker <<a href="mailto:report at bugs.python.org">report at bugs.python.org</a>><br>
<<a href="http://bugs.python.org/issue5211">http://bugs.python.org/issue5211</a>><br>
_______________________________________Python tracker <<a href="mailto:report at bugs.python.org">report at bugs.python.org</a>></blockquote>
More information about the Python-bugs-list
mailing list