[Python-bugs-list] Error in dictionary comparison (PR#282)
gpk@bell-labs.com
gpk@bell-labs.com
Sat, 8 Apr 2000 22:51:04 -0400 (EDT)
Full_Name: Greg Kochanski
Version: 1.5.2
OS: Solaris 2.6
Submission from: h-135-104-50-27.research.bell-labs.com (135.104.50.27)
# The following little chunk of python seems to reveal
# a bug in dictionary comparison or construction.
#
# As can be seen in the printout, the list of keys are
# the same (after sorting). All the values are one.
# Thus, the two dictionaries ought to compare equal.
# They don't.
#
# Smaller chunks of code don't seem to reveal the problem.
# python bug.py
# a.d= {10: 1, 4: 1, 3: 1, 2: 1, 11: 1} [2, 3, 4, 10, 11]
# b.d= {10: 1, 4: 1, 3: 1, 2: 1, 11: 1} [2, 3, 4, 10, 11]
# Traceback (innermost last):
# File "bug.py", line 34, in ?
# test()
# File "bug.py", line 30, in test
# assert equals(Set([2, 4, 3, 11, 10]), Set([2, 3, 4, 10, 11]))
# AssertionError
_dicttype = type({})
class Set:
def __init__(self, a = []):
if type(a) == _dicttype or type(a) == type(self):
self.d = a.copy()
else:
self.d = {}
for t in a:
self.add(t)
def add(self, item):
self.d[item] = 1
def copy(self):
return Set(self.d)
def equals(a, b):
ad = a.d.keys()
ad.sort()
print "a.d=", a.d, ad
bd = b.d.keys()
bd.sort()
print "b.d=", b.d, bd
return Set(b).d == Set(a).d
def test():
assert equals(Set([2, 4, 3, 11, 10]), Set([2, 3, 4, 10, 11]))
if __name__ == '__main__':
test()