Extending dict (dict's) to allow for multidimensional dictionary

R

Ravi

I can extend dictionary to allow for the my own special look-up tables. However now I want to be able to define multidimensional dictionary which supports look-up like this:

d[1]['abc'][40] = 'dummy'

and if d[1] and d[1][abc] raise KeyError just create them.

for d[1] I can override __getitem__() but how to do the same for the d[1][abc]?
 
P

Paul Rubin

Ravi said:
I can extend dictionary to allow for the my own special look-up
tables. However now I want to be able to define multidimensional
dictionary which supports look-up like this:

d[1]['abc'][40] = 'dummy'

Why do that anyway? You can use a tuple as a subscript:

d[1,'abc',40] = 'dummy'
 
J

John Nagle

Ravi said:
I can extend dictionary to allow for the my own special look-up
tables. However now I want to be able to define multidimensional
dictionary which supports look-up like this:

d[1]['abc'][40] = 'dummy'

Why do that anyway? You can use a tuple as a subscript:

d[1,'abc',40] = 'dummy'

Also, at some point, it's time to use a database.
If you find yourself writing those "dictionaries" to
files, or trying to look up everything with "abc"
in the second subscript, a database is appropriate.

John Nagle
 
T

TomF

Ravi said:
I can extend dictionary to allow for the my own special look-up
tables. However now I want to be able to define multidimensional
dictionary which supports look-up like this:

d[1]['abc'][40] = 'dummy'

Why do that anyway? You can use a tuple as a subscript:

d[1,'abc',40] = 'dummy'

Depends on the use cases. If you only want to access d by a full
tuple, that's fine. If you want to access all the entries below d[1],
it's a problem. And if you want to access entities randomly by an
arbitrary dimension, you probably want a database (as others have
pointed out).

-Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top