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

Maarten Keijzer mkeijzer at cs.vu.nl
Mon Feb 18 10:01:51 EST 2002


Hi,

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):
    pass

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

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



More information about the Python-list mailing list