Re: Identity dictionnary

Discussion in 'Python' started by Bob Ippolito, Feb 29, 2004.

  1. Bob Ippolito

    Bob Ippolito Guest

    On 2004-02-28 21:05:48 -0500, Jean-Sebastien Roy <> said:

    > I'm trying to create an object to be used as a key in a dictionary,
    > where comparison (for key retrieval) would be done by 'is' instead of
    > __eq__.
    > (__eq__ is defined in this object, but serve another purpose and does
    > not return a boolean).
    >
    > Is defining __hash__ as id(self) sufficient to guarantee that __eq__
    > will never be called by the dict implementation ? (it seems to work in
    > practice)
    > If not, how to ensure __eq__ will never be called ?


    I don't believe your assumption about dict is correct. A typical dict
    is not going to have 2**32 slots in it, so you will eventually get a
    collision, which will call __eq__ unless the key you're getting happens
    to be the first item in that slot (it think uses object identity first
    as an optimization and for sanity, though I have not looked at the
    source).

    What you might want is a special kind of container, not a special kind
    of object to use as keys to a dict:

    class identitymap(object):
    def __init__(self):
    self._dict = {}

    def __getitem__(self, item):
    return self._dict[id(item)][1]

    def __setitem__(self, item, value):
    self._dict[id(item)] = (item, value)

    .... etc.

    I have used a data structure such as this before for similar reasons
    (an attempt at pure python transparent object persistence..).

    -bob
    Bob Ippolito, Feb 29, 2004
    #1
    1. Advertising

  2. In article <2004022821314075249%bob@redivicom>,
    Bob Ippolito <> wrote:

    > > Is defining __hash__ as id(self) sufficient to guarantee that __eq__
    > > will never be called by the dict implementation ? (it seems to work in
    > > practice)
    > > If not, how to ensure __eq__ will never be called ?

    >
    > I don't believe your assumption about dict is correct. A typical dict
    > is not going to have 2**32 slots in it, so you will eventually get a
    > collision, which will call __eq__ unless the key you're getting happens
    > to be the first item in that slot (it think uses object identity first
    > as an optimization and for sanity, though I have not looked at the
    > source).


    A few years ago, Tim Peters said that == what not called unless __hash__
    are equals (whatever small the number of slots is (quite strange indeed,
    but why not ?)).

    But that may not be true anymore (that's why I asked).

    > What you might want is a special kind of container, not a special kind
    > of object to use as keys to a dict:
    >
    > class identitymap(object):
    > def __init__(self):
    > self._dict = {}
    >
    > def __getitem__(self, item):
    > return self._dict[id(item)][1]
    >
    > def __setitem__(self, item, value):
    > self._dict[id(item)] = (item, value)
    >
    > ... etc.
    >
    > I have used a data structure such as this before for similar reasons
    > (an attempt at pure python transparent object persistence..).


    I initialy ruled out reimplementing a special dictionnary because I'm a
    bit worried about the performance, but that's a nice solution : thanks !
    I'll probably go with it.

    Regards,

    js
    Jean-Sebastien Roy, Feb 29, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giovanni Bassi
    Replies:
    0
    Views:
    627
    Giovanni Bassi
    Oct 20, 2003
  2. NOBODY
    Replies:
    2
    Views:
    795
    Thomas Weidenfeller
    Oct 17, 2003
  3. Sophie Alléon

    newbie question about dictionnary ?

    Sophie Alléon, Sep 5, 2003, in forum: Python
    Replies:
    9
    Views:
    309
    Bengt Richter
    Sep 5, 2003
  4. Famille Delorme

    Dictionnary vs Class for configuration

    Famille Delorme, Apr 30, 2004, in forum: Python
    Replies:
    7
    Views:
    322
  5. Sigfried
    Replies:
    9
    Views:
    457
    Tom Anderson
    Dec 2, 2008
Loading...

Share This Page