Generating getter methods with exec

Stefan Schwarzer s.schwarzer at ndh.net
Mon Mar 18 16:58:59 EST 2002


Hello Neal

thank you for your answer. :)

Neal Norwitz wrote:
> You don't need to generate code.  This should do what you want:
> 
> class GetterTest:
>     def __init__(self):
>         self._dict = {'key1': 'one',
>                       'key2': 'two',
>                       'key3': 'three'}
> 
>     def __getattr__(self, attr):
>         try:
>             return self._dict[attr]
>         except KeyError:
>             raise AttributeError, "getting attribute '%s'" % attr
> 
> >>> g = GetterTest()
> >>> g.key1
> 'one'
> >>> g.key2
> 'two'
> >>> g.key3
> 'three'
> >>> g.key4
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 8, in __getattr__
> AttributeError: getting attribute 'key4'

Yes, that's the straightforward solution. :-)

On the other hand, what I posted was a simplified example. Finally, I
will have several dictionary-like objects and not all of their items
should be accessible by get/set methods (only some of the corresponding
attributes belong to the public interface). I'm a bit afraid the overhead
of dynamically making all the decisions for every access might be slow
(though it may well be that all will be fast enough).

So to rephrase the problem a bit: I would like to generate simple getters
and setters with rather little decision overhead during the actual accesses.

Stefan



More information about the Python-list mailing list