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

2007-04-03 Thread Bruno Desthuilliers
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 __in

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

2007-04-03 Thread Birone Lynch
> 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. Were you thinking of get: a.get(k[, x]) returns a[k] if k in a, else x But do you want to lookup

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

2007-04-03 Thread abcd
> 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): > r

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

2007-04-03 Thread irstas
On Apr 3, 7:26 pm, "abcd" <[EMAIL PROTECTED]> 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 whic

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

2007-04-03 Thread abcd
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