Retrieve an item from a dictionary using an arbitrary object as the key

Discussion in 'Python' started by abcd, Apr 3, 2007.

  1. abcd

    abcd Guest

    Hi,
    I have a class such as,

    class Type:
    def __init__(self, val):
    self.val = val

    class Person:
    def __init__(self, name, age):
    self.name = name
    self.age = age

    So I have a dictionary which maps an instance of Type to an instance
    of Person. Now I need to retrieve a particular Person given a Type
    object. What method in Type do I need to implement to allow it to be
    retrieved?

    For example (this is just an example):

    t = Type(19)
    p = Person("bob", 99)

    x = {t : p}

    .....<later on in the code somewhere>....

    def getPerson(val):
    return x[Type(val)]

    getPerson(19) ....should return me the Person with name "bob" and age
    99. I am thinking there is some method that is used by the dictionary
    to know if the key exists, just not sure which.

    thanks
     
    abcd, Apr 3, 2007
    #1
    1. Advertising

  2. abcd

    Guest

    On Apr 3, 7:26 pm, "abcd" <> wrote:
    > Hi,
    > I have a class such as,
    >
    > class Type:
    > def __init__(self, val):
    > self.val = val
    >
    > class Person:
    > def __init__(self, name, age):
    > self.name = name
    > self.age = age
    >
    > So I have a dictionary which maps an instance of Type to an instance
    > of Person. Now I need to retrieve a particular Person given a Type
    > object. What method in Type do I need to implement to allow it to be
    > retrieved?
    >
    > For example (this is just an example):
    >
    > t = Type(19)
    > p = Person("bob", 99)
    >
    > x = {t : p}
    >
    > ....<later on in the code somewhere>....
    >
    > def getPerson(val):
    > return x[Type(val)]
    >
    > getPerson(19) ....should return me the Person with name "bob" and age
    > 99. I am thinking there is some method that is used by the dictionary
    > to know if the key exists, just not sure which.
    >
    > thanks


    You'll need __eq__ for testing if two objects are equivalent, and
    __hash__ for calculating object's hash value.

    class Type:
    def __init__(self, val):
    self.val = val

    def __eq__(self, other):
    return self.val == other.val

    def __hash__(self):
    return hash(self.val)
     
    , Apr 3, 2007
    #2
    1. Advertising

  3. abcd

    abcd Guest

    > You'll need __eq__ for testing if two objects are equivalent, and
    > __hash__ for calculating object's hash value.
    >
    > class Type:
    > def __init__(self, val):
    > self.val = val
    >
    > def __eq__(self, other):
    > return self.val == other.val
    >
    > def __hash__(self):
    > return hash(self.val)


    that's exactly what I needed, thanks.
     
    abcd, Apr 3, 2007
    #3
  4. Re: Retrieve an item from a dictionary using an arbitrary objectas the key

    abcd a écrit :
    >>You'll need __eq__ for testing if two objects are equivalent, and
    >>__hash__ for calculating object's hash value.
    >>
    >>class Type:


    While we're at it, and unless you're stuck with an aging Python version,
    make Type and Person new-style classes:

    class Type(object):

    >> def __init__(self, val):
    >> self.val = val
    >>
    >> def __eq__(self, other):
    >> return self.val == other.val
    >>
    >> def __hash__(self):
    >> return hash(self.val)

    >
    >
    > that's exactly what I needed, thanks.
    >


    And to answer your original question, ie "I am thinking there is some
    method that is used by the dictionary to know if the key exists, just
    not sure which.", you can:

    - test if a dict as a given key:

    d = {"a" : 42, "b" : "foo"}
    "a" in d
    => True
    "x" in d:
    => False

    - and/or use dict.get(key, default=None):

    d.get("a")
    => 42
    d.get("x")
    => None
    d.get("x", "woops")
    => "woops"

    HTH
     
    Bruno Desthuilliers, Apr 3, 2007
    #4
    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. Honestmath
    Replies:
    5
    Views:
    559
    Honestmath
    Dec 13, 2004
  2. Guest
    Replies:
    2
    Views:
    5,578
    wwwtar
    Nov 2, 2006
  3. Egor Bolonev

    [dictionary] how to get key by item

    Egor Bolonev, Dec 14, 2004, in forum: Python
    Replies:
    11
    Views:
    525
    Skip Montanaro
    Dec 14, 2004
  4. avital

    Item has already been added. Key in dictionary

    avital, Dec 12, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    524
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Dec 19, 2006
  5. Samuel
    Replies:
    8
    Views:
    122
    [MSFT]
    Oct 14, 2004
Loading...

Share This Page