repr(x) == repr(y) <=> x == y AND eval(repr(x)) == x

Erik Max Francis max at alcyone.com
Fri Oct 18 15:30:14 EDT 2002


Thorsten Kampe wrote:

> I'm using dictionaries for some "very generalized programs". This
> means that I cannot tell in advance if all the items I'm operating on
> are hashable. The "trick" that came to my mind was to use repr(x) as
> the key (instead of x).
> 
> This relies on two assumptions:
> 
> 1. repr(x) == repr(y) <=> x == y

An overridden __repr__ might not qualify, but the default one
(containing the class name and the id of the object) would.

> 2. eval(repr(x)) == x (for further processing)

Any user-defined instance that defines __hash__ but doesn't do very
special tricks with __repr__ would not qualify.  Which potentially means
you're leaving out a huge group of potential objects.

If you're interested in whether or not the objects are hashable, why not
just try calculating their hashes, and then handle the error should one
happen?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Consistency is the last refuge of the unimaginative.
\__/ Oscar Wilde
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.



More information about the Python-list mailing list