[IronPython] Tuple Hashing and Dictionaries

Michael Foord michael.foord at resolversystems.com
Wed Jun 14 07:03:08 PDT 2006


Hello all,

We have created a custom object which we wanted to use as a dictionary 
key as part of a tuple.

To make sure that equivalent instances of our class can be used as keys 
we overloaded the __hash__ method.

This works in CPython, but not in IronPython. The following test 
sessions both use the following class :

class Test(object):

    def __hash__(self):
        return hash("fish")
    def __eq__(self, other):
        return type(other) == type(self)


CPython :

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> a = Test()
 >>> b = Test()
 >>> a == b
True
 >>> c = {}
 >>> c[(a, 1, 2)] = 3
 >>> c[(b, 1, 2)]
3

IronPython :

IronPython 1.0.60420 (Beta) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
 >>> a = Test()
 >>> b = Test()
 >>> a == b
True
 >>> c = {}
 >>> c[(a, 1, 2)] = 3
 >>> c[(b, 1, 2)]
Traceback (most recent call last):
  File , line 0, in input##22
KeyError: '(<Test object at 0x000000000000002B>, 1, 2)'

All the best,


Michael Foord
http://www.resolversystems.com
http://www.voidspace.org.uk/python/index.shtml






More information about the users mailing list