[Python-Dev] Keep default comparisons - or add a second set? (original) (raw)

Noam Raphael noamraph at gmail.com
Wed Dec 28 14:29:03 CET 2005


And another example:

a = 1+2j b = 2+1j a > b Traceback (most recent call last): File "", line 1, in TypeError: no ordering relation is defined for complex numbers

I came to think that, when forgetting backwards compatibility for a while, the best thing for comparison operators to do is to raise a TypeError by default, and work only for types that it makes sense to compare. I think it is more "explicit is better than implicit", and I have now two reasons for that:

  1. Things like "Decimal(3.0) == 3.0" will make more sense (raise an exception which explains that Decimals should not be compared to floats, instead of returning false constantly).
  2. It is more forward compatible - when it is discovered that two types can sensibly be compared, the comparison can be defined, without changing an existing behaviour which doesn't raise an exception.

Perhaps you can explain to me again why arbitrary objects should be comparable? I don't see why sorting objects according to values should work when the order has no real meaning. I don't see why you need all objects to be comparable for storing them in containers with the behaviour of dict or set.

If the reason is that you want containers that work among multiple sessions, and are "identity-based" (that is, only one object can be used as a key), you can attach to each object an id that isn't session dependent, and use that instead of the default memory address.

It may be a reason for dropping the default "hash is id": suppose that you want a persistent storage that will work like dicts but will not be associated with one Python session (it may be exactly Zope's BTrees

In fewer words, the built-in id() is just one way to assign identities to objects. hash shouldn't use it implicitly when there's no value-based hash value - if it wouldn't, the rule that x == y -> hash(x) == hash(y) will be preserved also between different sessions, so persistent objects would be able to use hash values.

Does it make sense to you?

Noam



More information about the Python-Dev mailing list