Entry
Extracting tuple values from a dictionary
Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 71, Magnus L. Hetland
"""
Packages: basic_datatypes.dictionaries
"""
"""
> For the specific example I gave, formatting works fine, but what about
> other situations, such as extracting a tuple of values from a hash: a,
> b, c = value{key1,key2,key3}
It seems you may want to define a dictionary of your own, here...
"""
from types import TupleType
from UserDict import UserDict
class TupleDict(UserDict):
def __init__(self,data):
UserDict.__init__(self)
self.data = data
def __getitem__(self,key):
if type(key) is TupleType:
result = []
for k in key:
result.append(self[k])
return tuple(result)
return self.data[key]
"""
That should work the way you want.
"""