How to implement 2-D array using UserDict?

Alex Martelli alex at magenta.com
Thu Aug 3 03:51:30 EDT 2000


"Sam Wun" <swun at eSec.com.au> wrote in message
news:3988F281.90E8F063 at eSec.com.au...
> I want to do something like:
>
> import UserDict
> dict = UserDict.UserDict()
> dict["Section"]["key"] = "some value"
>
> How can I do that in Python?

You may derive a class from UserDict to do that, I guess, but
it would seem more natural to me to use plain dicts here, and
a tuple as the index:

    dict={}
    dict['Section','key'] = "some value"

You can do that with a UserDict just as well, of course.  But
is there some reason you really need the multiple-brackets
syntax?  The tuple-based syntax Python offers appears to be
more natural to me.


Alex







More information about the Python-list mailing list