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

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Mon Oct 25 07:10:06 EDT 2004


Doran_Dermot at emc.com 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:

http://www.python.org/2.2.2/descrintro.html#property

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