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

Doran_Dermot at emc.com Doran_Dermot at emc.com
Mon Oct 25 13:37:21 CEST 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
Title"

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: python-list-bounces+doran_dermot=emc.com at python.org
[mailto:python-list-bounces+doran_dermot=emc.com at python.org]On Behalf Of
Michael Hoffman
Sent: 25 October 2004 12:10
To: python-list at python.org
Subject: Re: Implementing class attribute access methods via
pseudo-function o verloading.


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
-- 
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list