Defining accessor methods

Greg Ewing greg at cosc.canterbury.ac.nz
Mon Jun 25 23:17:44 EDT 2001


Graham Ashton wrote:
> 
> I thought of having get_my_attribute() and set_my_attribute() but I don't
> really like the idea because it just makes code that uses them more
> (unnecessarily) long winded, and the author of the above URL advises
> against it (though they don't say why).

They're talking about Smalltalk, and in that context it
*is* unnecessarily long-winded to include 'get' and 'set'
in the method names. What they do suggest, i.e.

   obj attrname
   obj attrname: value

works well in Smalltalk, and is a standard idiom used 
throughout the Smalltalk system itself, which is probably 
why they recommend it.

Note that those are two separate methods, with different
names (the ':' is part of the method name in Smalltalk).
It doesn't imply that translating this idiom to Python
requires using a single method with a variable argument
list.

The convention I would recommend in Python is 

   obj.attrname()
   obj.set_attrname(value)

Since getting attributes tends to be more common than
setting them, this isn't unbearably verbose, and I find
that having 'set' in the setting method's name helps to
make the code read more explicitly.

> I've not noticed any use of accessor methods in sample Python code
> (i.e. in books/tutorials); do people use them much in Python?

I make extensive use of them in the API of my Python GUI
project (http://www.cosc.canterbury.ac.nz/~greg/python_gui/).
I want the API to be as future-proof as possible, so all
publically-visible attributes of my GUI objects are accessed
through methods. I use the convention I recommended above,
plus a couple of others. One is that the constructors of
my objects allow initial values to be given for all its 
attributes using keyword arguments, so that e.g.

  b = Button(width = 42, height = 17, text = "Boo!")

is equivalent to

  b = Button()
  b.set_width(42)
  b.set_height(17)
  b.set_text("Boo!")

My objects also have a set() method which takes keyword
arguments in the same way, so if you want to change a
bunch of attributes you can set them all in one go with
a statement like

  b.set(width = 200, text = "Start Spanish Inquisition")

These two things help to cut down a lot on the verbosity
of using set_xxx methods.

I tend not to use accessor methods so much in code which
is internal to an application or library. Whether this is
a good or bad habit is a matter of judgement. It's a
tradeoff between time spent writing and using the accessor
methods, and time spent revising the rest of the code when
the implementation of an attribute changes. 

My experience suggests that, for internal code, it's usually 
easier not to bother with accessor methods until a clear need 
for them arises. But, as they say, your kilometerage may
vary.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list