Implementing class attribute access methods via pseudo-functi on o verloading.

Doran_Dermot at Doran_Dermot at
Mon Oct 25 07:37:21 EDT 2004

Hi Michael,

First of all thanks for letting some light penetrate the rock I've been
hiding under :-) I'd completely ignored or forgotten about Properties.  I'll
study them and see if I like them.

It's interesting to note that you prefer code like: theProblem.title = "The

However, I wonder if people coming from the Java/C++ world prefer:
theProblem.title( "The Title" )

Obviously the first version is less typing! However, the second version
might feel more natural to Java/C++ programmers.  Just an observation!

Cheers and thanks again!!

-----Original Message-----
From: at
[ at]On Behalf Of
Michael Hoffman
Sent: 25 October 2004 12:10
To: python-list at
Subject: Re: Implementing class attribute access methods via
pseudo-function o verloading.

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