fixedpoint cmp conundrum
Tim Peters
tim.one at comcast.net
Mon May 3 22:02:53 EDT 2004
[Robert Brewer]
> >>> import fixedpoint
> >>> a = fixedpoint.FixedPoint(3, 2)
> >>> bunch = [None, 12, a]
...
> >>> bunch.index(a)
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> File "C:\Python23\Lib\fixedpoint.py", line 316, in __cmp__
> xn, yn, p = _norm(self, other, FixedPoint=type(self))
> File "C:\Python23\Lib\fixedpoint.py", line 482, in _norm
> y = FixedPoint(y, x.p)
> File "C:\Python23\Lib\fixedpoint.py", line 255, in __init__
> raise TypeError("can't convert to FixedPoint: " + `value`)
> TypeError: can't convert to FixedPoint: None
>
>
> Mmrph! I could fix this by:
>
> 1. Iterating through "bunch" by hand and trapping TypeError,
> 2. Overriding or hacking the __cmp__ function in fixedpoint, or
> 3. Overriding or hacking the _norm function in fixedpoint
>
> The question is: given that there are multiple places to "fix" this,
> where should I do so?
4. Define an __eq__ method, which returns False if its arguments
are non-comparable.
FixedPoint was created before rich comparisons were available. Examples of
new types in Python 2.3 that take approach #4 are the sets.* and datetime.*
types; e.g.,
>>> from sets import Set
>>> s = Set([12])
>>> s == 12
False
>>> cmp(s, 12)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python23\Lib\sets.py", line 330, in __lt__
self._binary_sanity_check(other)
File "C:\Python23\Lib\sets.py", line 343, in _binary_sanity_check
raise TypeError, "Binary operation only permitted between sets"
TypeError: Binary operation only permitted between sets
>>>
More information about the Python-list
mailing list