[Python-Dev] RE: [Python-checkins] python/dist/src/Lib weakref.py,1.19,1.20

Raymond Hettinger python@rcn.com
Sun, 25 May 2003 03:08:33 -0400


Arghh.  One more example always arises after going to bed.

The original required only equality.  The new version requires
equality, hashability, *and* weak referencability.

Python 2.3b1 (#40, Apr 25 2003, 19:06:24) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> import weakref
>>> class One:
 def __eq__(self, other): return other == 1
 def __hash__(self, other):  return hash(1)
>>> wkd = weakref.WeakKeyDictionary()
>>> o = One()
>>> wkd[o] = 1
>>> len(wkd)
1
>>> del wkd[1]
>>> len(wkd)
0


Python 2.3b1+ (#40, May 23 2003, 00:08:36) [MSC v.1200 32
Type "help", "copyright", "credits" or "license" for more
>>> class One:
...     def __eq__(self,other):  return other==1
...     def __hash__(self):  return hash(1)
...
>>> import weakref
>>> wkd = weakref.WeakKeyDictionary()
>>> o = One()
>>> wkd[o] = 1
>>> len(wkd)
1
>>> del wkd[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "C:\PY23\lib\weakref.py", line 167, in __delitem__
    del self.data[ref(key)]
TypeError: cannot create weak reference to 'int' object
>>> len(wkd)
1



Raymond