Annoying behaviour of the != operator
David M. Cooke
cookedm+news at physics.mcmaster.ca
Fri Jun 10 13:53:27 EDT 2005
Robert Kern <rkern at ucsd.edu> writes:
> greg wrote:
>> David M. Cooke wrote:
>>
>>>>To solve that, I would suggest a fourth category of "arbitrary
>>>>ordering", but that's probably Py3k material.
>>>
>>>We've got that: use hash().
>>>[1+2j, 3+4j].sort(key=hash)
>> What about objects that are not hashable?
>> The purpose of arbitrary ordering would be to provide
>> an ordering for all objects, whatever they might be.
>
> How about id(), then?
>
> And so the circle is completed...
Or something like
def uniquish_id(o):
try:
return hash(o)
except TypeError:
return id(o)
hash() should be the same across interpreter invocations, whereas id()
won't.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
More information about the Python-list
mailing list