Generating getter methods with exec

Stefan Schwarzer s.schwarzer at ndh.net
Mon Mar 18 15:29:14 EST 2002


I would like to generate some getter methods in a class which in fact
access a stored dictionary. Unfortunately, I'm not able to get it work.

Suppose the following code is in getter_test.py:

class GetterTest:
    def __init__(self):
        self._dict = {'key1': 'one',
                      'key2': 'two',
                      'key3': 'three'}

    def _make_getters(self, dict_name, key_list):
        for key in key_list:
            code = ( "def %(key)s(self):\n"
                     "    return self.%(dict_name)s['%(key)s']\n" %
                     vars() )
            print code
            # this seems to be wrong but what should that be?
            exec code in globals(), self.__dict__

    def key3(self):
        return self._dict['key3']

In the interpreter (Python 2.2) I get:

>>> import getter_test
>>> gt = getter_test.GetterTest()
>>> gt._make_getters('_dict', ['key1', 'key2'])
def key1(self):
    return self._dict['key1']

def key2(self):
    return self._dict['key2']
>>> gt.key1()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: key1() takes exactly 1 argument (0 given)

As can be seen, gt.key1() doesn't behave as expected (at least as
expected by me ;-) ).

>>> gt.key1(gt)
'one'

This works, but needs an additional instance parameter which corresponds
to this observation:

>>> gt.key1
<function key1 at 0xa662c>

though I would rather like

>>> gt.key3
<bound method GetterTest.key3 of <getter_test.GetterTest instance at 0xa648c>>

Can anyone tell me how I can generate the evaluated code as bound
methods instead of functions? Many thanks in advance for any help! :-)

Stefan



More information about the Python-list mailing list