__hash__がなくてもハッシュに入ることもある
http://d.hatena.ne.jp/morchin/20071130/p1 から。
>>> class Foo: pass >>> f = Foo() >>> d = {} >>> d[f] = 1 >>> d[f] 1 >>> hash(f) 32015344 >>> id(f) 32015344
ほおーー。
object.c内のhash部分のコードはこんな感じ:
long PyObject_Hash(PyObject *v) { PyTypeObject *tp = v->ob_type; if (tp->tp_hash != NULL) return (*tp->tp_hash)(v); if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { return _Py_HashPointer(v); /* Use address as hash value */ } /* If there's a cmp but no hash defined, the object can't be hashed */ PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", v->ob_type->tp_name); return -1; }
比較の方法が定義されているにもかかわらずハッシュ関数が定義されていない場合のみエラーとか、なんでこういう実装になっているのか謎だ。