Python 2.2, creating new types from base types
Terry Reedy
tjreedy at home.com
Thu Jan 17 20:53:55 EST 2002
"Peter Milliken" <peter.milliken at gtech.com> wrote in message
news:a27hn1$tph1 at news1.gtech.com...
> I would like to create a specialised data type that is essentially a
> dictionary of dictionaries
...
> class my_dict (dict):
> def __init__ (self):
> self.special_dict = {'1' : {'2' : {'3' : '4'}}} # Example
data
>
> Accessing the contents of an instance should (to my way of thinking
:-)) be
> something like this:
>
> abc = my_dict()
>
> print abc['1']['2']['3']
>
>
> Now, I wasn't sure how to overload the __getitem__ def
You seem to be hoping/presuming that your __getitem__() will somehow
get all three keys at once. It won't. However....
I believe you could access with tuple of keys and have your magic
access function split it and work down thru the dictionary tree. ie:
print abc[('1','2','3')]
def __getitem__(self, keytuple): #UNTESTED!!!
value = self.special_dict
for key in keytuple:
value = value[key]
return value
Terry J. Reedy
More information about the Python-list
mailing list