2

I am mostly interested in terms of what is happening but lets say I have a custom class with __eq__ implemented and nothing else.

How do the other comparisons get resolved? What is the default behaviour? Every time I run the comparison I get a different result. I know that I need to implement these and that I can also use @total_ordering to simplify the job.

What is actually happening behind the scenes? This is mostly out curiosity.

Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101

2 Answers2

5

If __lt__ isn't defined, comparisons use __cmp__ instead. If that's not defined, it uses a default comparator. And it appears that the default comparator compares by "object identity (address)".

See the docs for __cmp__ for details.

cHao
  • 84,970
  • 20
  • 145
  • 172
1

Some additional information from this section of the Python documentation:

Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result).

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

Community
  • 1
  • 1
Tyler MacDonell
  • 2,609
  • 18
  • 27