Implementing class attribute access methods via pseudo-function o verloading.

Michael Hoffman at
Mon Oct 25 13:10:06 CEST 2004

Doran_Dermot at wrote:
> I've seen lots of code in which the attributes of a class are accessed and
> modified using two separate methods.  For example:

I think this is a great illustration of what properties can be used for:

Then you can reduce your class to:

class Problems:
     def __init__(self, refNum):
         self._refNum = refNum

This is a LOT less code.

And use theProblem.title, theProblem.problem without any apparent 
function calls. As far as those fancy accessor methods go, YAGNI. If you 
do need them later, you can always add them:

     def _get_title(self):
         return self._title

     def _set_title(self, title):
         assert isinstance(title, basestring)
         self._title = title

     title = property(_get_title, _set_title)

I would much rather use theProblem.title = "the title" rather than 
theProblem.title("the title").
Michael Hoffman

More information about the Python-list mailing list