[Python-bugs-list] [ python-Bugs-475877 ] Mutable subtype instances are hashable
noreply@sourceforge.net
noreply@sourceforge.net
Sun, 28 Oct 2001 19:28:56 -0800
Bugs item #475877, was opened at 2001-10-28 19:24
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=475877&group_id=5470
Category: Type/class unification
Group: Python 2.2
Status: Open
Resolution: None
Priority: 7
Submitted By: Tim Peters (tim_one)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Mutable subtype instances are hashable
Initial Comment:
>>> class D(dictionary): pass
...
>>> d = D()
>>> hash(d)
7920544
>>> id(d)
7920544
>>>
Ditto for instances of list subclasses:
>>> class L(list): pass
...
>>> x = L(range(100))
>>> hash(x)
7928992
>>> id(x)
7928992
>>>
Among other nasties, this lets them get used as
mutable dict keys.
Reported by Mark J on c.l.py:
"""
From: Mark J <maj64@hotmail.com>
Sent: Sunday, October 28, 2001 10:03 PM
To: python-list@python.org
Subject: Python 2.2b1 hashable dictionary bug?
Since I haven't had a good hit rate at detecting bugs
vs. features, I thought I'd post here before filing a
bug report at SourceForge.
Python 2.2b1 (#1, Oct 19 2001, 23:11:09)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on
linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> class D(dictionary): pass
...
>>> d = {}
>>> d2 = D()
>>> d[d2] = "dictionary used as key"
>>> d
{{}: 'dictionary used as key'}
>>> d[D()]="now have two keys that look the same"
>>> d
{{}: 'now have two keys that look the same',
{}: 'dictionary used as
key'}
>>> d2["key"] = "mutable key in d"
>>> d
{{}: 'now have two keys that look the same',
{'key': 'mutable key in
d'}: 'dictionary used as key'}
>>> d[d] = "plain dictionary not allowed as key"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unhashable type
>>> d2[d2] = "subclassed dictionary allowed though"
>>> d2
{{...}: 'subclassed dictionary allowed
though', 'key': 'mutable keys'}
>>> #whoa: what just happened: {...} ??
Interesting.... So is this a feature or a bug?
"""
----------------------------------------------------------------------
>Comment By: Tim Peters (tim_one)
Date: 2001-10-28 19:28
Message:
Logged In: YES
user_id=31435
Jeez, hash() has gone off the deep end:
>>> import sys
>>> hash(sys.stdin)
7690032
>>>
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=475877&group_id=5470