Getters and Setters

Neil Schemenauer nascheme at ucalgary.ca
Thu Jul 15 18:25:25 EDT 1999


Bernhard Herzog <herzog at online.de> wrote:
>So, putting the functions into the class would be better:
>
>              def getter(self, key=name[3:]):
>                  return getattr(self, key)
>              setattr(self.__class__, name, getter)
>              return getattr(self, name)

This is a very nice solution.  Thanks Bernhard.  It is a little
slower than Tim's method but avoids creating circular references
and doesn't create a function for each instance.  It also works
for class variables.


    Neil




More information about the Python-list mailing list