[Edu-sig] python versus __python__

Scott David Daniels Scott.Daniels at Acm.Org
Mon Oct 24 01:58:37 CEST 2005


Kirby Urner wrote:
>>Kirby
>>
>>PS:  since you've been studying subclassing primitive types using __new__,
>>maybe you could show us a user-defined floating point type that reports
>>two numbers equal if their absolute value difference is less than e.  
>>Anyone?
>>
> 
Fuzzy redone a breath:
class Fuzzy(float):
     tol = 1e-8
     def __new__(cls, arg=0.0):
         return float.__new__(cls, arg)
     def __eq__(self, other):
         return abs(self - other) < self.tol
> ...

Here's the problem:
    Your notion of equality is not transitive.  That's tough -- it
invalidates the assumptions of the float type (and all basic types).
Also, you didn't tweak hash, so a dictionary with these things in them
would not find entries that turn out to be equal.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Edu-sig mailing list