There is more than one way to do it - and for no apparent reason.

Maarten Keijzer mkeijzer at
Mon Feb 18 16:01:51 CET 2002


if it's syntactic sugar you want, create a base class that uses __getitem__ and 
__setitem__. For instance like this:

class MyBase:
    def __getitem__(self, item):
        return self.__dict__[item]

    def __setitem__(self, item, value):
        self.__dict__[item] = value

Then you can do:

class MyDerived(MyBase):

base = MyDerived() = 'hi'
print base['name']
base['name'] = 'ho'

If you have other uses for the [] notation, you're still free to use it.

More information about the Python-list mailing list