unorderable error: less ok, equal ok, less-or-equal gives unorderable error!
RainyDay
andrei.avk at gmail.com
Mon Jun 30 16:29:53 EDT 2014
On Monday, June 30, 2014 3:34:25 PM UTC-4, Peter Otten wrote:
> RainyDay wrote:
>
>
>
> > Hi, in python 3.4.1, I get this surpising behaviour:
>
> >
>
> >>>> l=Loc(0,0)
>
> >>>> l2=Loc(1,1)
>
> >>>> l>l2
>
> > False
>
> >>>> l<l2
>
> > True
>
> >>>> l<=l2
>
> > Traceback (most recent call last):
>
> > File "<stdin>", line 1, in <module>
>
> > TypeError: unorderable types: Loc() <= Loc()
>
> >>>> l==l2
>
> > False
>
> >>>> l<l2 or l==l2
>
> > True
>
> >
>
> > Loc implements both __lt__ and __eq__, which should be enough (?),
>
>
>
> These two methods should be sufficient if you use the
>
> functools.total_ordering class decorator, see
Thanks! I literally failed to read one more paragraph in a SO answer
which referenced this decorator. I really need to start reading those
paragraphs, they often provide answers...
> > but even after I've added __lte__, I still have the error.
>
>
>
> There is no special method of that name; it should probably be __le__().
Right, I used lte in django and assumed they were consistent with python.
> > def __eq__(self, other):
>
> > return self._loc == getattr(other, "_loc", None)
>
>
>
> Note that None is not a good default when _loc is expected to be a tuple:
>
I'm only using None in equality comparison, it's never a default value of
_loc itself, so this should be ok because it'll compare to all other object
types and correctly say they're unequal.
- andrei
More information about the Python-list
mailing list