Author gumtree
Recipients gumtree
Date 2008-08-29.22:39:08
SpamBayes Score 6.1129e-09
Marked as misclassified No
Message-id <1220049550.4.0.0881969750537.issue3734@psf.upfronthosting.co.za>
In-reply-to
Content
The following is quoted from the Python docs (ref/numeric_types):

"Note: If the right operand's type is a subclass of the left operand's
type and that subclass provides the reflected method for the operation,
this method will be called before the left operand's non-reflected
method. This behavior allows subclasses to override their ancestors'
operations."

My issue is that the built-in complex type does not respect this rule
(see code below). Note that float can be subclassed using the method
shown below and the rules are applied correctly. It seems that it is
only a problem with complex.

class xcomplex( complex ):

    def __new__(cls,*args,**kwargs):
        return complex.__new__(cls,*args,**kwargs)

    def __coerce__(self,other):
        t = complex.__coerce__(self,other)
        try:
            return (self,xcomplex(t[1]))
        except TypeError:
            return t

    def __add__(self,x):
        return xcomplex( complex.__add__(self,x) )

    def __radd__(self,x):
        return xcomplex( complex.__radd__(self,x) ) 

print type(z + xz)  # gives complex when xcomplex is required
History
Date User Action Args
2008-08-29 22:39:10gumtreesetrecipients: + gumtree
2008-08-29 22:39:10gumtreesetmessageid: <1220049550.4.0.0881969750537.issue3734@psf.upfronthosting.co.za>
2008-08-29 22:39:09gumtreelinkissue3734 messages
2008-08-29 22:39:08gumtreecreate