Hello people,

Is there any built-in way to know if an object is a valid dictionary key ? From 
what I know, the object must be hashable, and from the python doc, an object is 
hashable if it has the __hash__ and (__cmp__ or __eq__) methods.

http://docs.python.org/2/glossary.html#term-hashable

I found this on the net, but considering the above definition, does it really 
check the cmp/eq existence ?

def ishashable(x):
    try:
        hash(x)
    except TypeError:
        return False
    else:
        return True

I was trying to know if any custom class can be used as a dict key. It looks 
like you can. Yet I'm a little bit concerned, because last time I used invalid 
objects as keys, I got a bug that was really difficult to spot.

Regards,

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to