automatic accessors to a member var dict elements?

Christopher J. Bottaro cjbottaro at alumni.cs.utexas.edu
Thu Oct 14 21:29:12 CEST 2004


If I have the following class:

class MyClass:
        def __init__(self):
                m_dict = {}
                m_dict['one'] = 1
                m_dict['two'] = 2
                m_dict['three'] = 3

Is there anyway to generate automatic accessors to the elements of the dict? 
For example, so I could say:

obj = MyClass()
obj.one # returns obj.my_dict['one']
obj.one = 'won' # same as obj.my_dict['one'] = 'won'

By automatic, I mean so I don't have to write out each method by hand and
also dynamic, meaning if m_dict changes during runtime, the accessors are
automatically updated to reflect the change.

Thanks for the help.




More information about the Python-list mailing list