Multikey Dict?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Nov 12 21:02:15 EST 2005


Sam Pointon a écrit :
>>If I could just say to Python: john and graham (and ...) are all a part
>>of a "superdict" and either their id or their name can be used as keys.
> 
> 
>>Can I do that somehow?
> 
> 
> Sure you can. There are two obvious ways to do this - enlist the aid of
> a Superdict (or similar) class to take care of the details. A simple
> implementation would be:
> 
> class Superdict(object):
> 
>     def __init__(self, by_id, by_name):
>         self.by_id = by_id
>         self.by_name = by_name
> 
>     def __getitem__(self, value):
>         if value.isdigit():
>             return self.by_id[value]
>         else:
>             return self.by_name[value]

What if the OP now wants non numeric id fields ?




More information about the Python-list mailing list