Defining accessor methods

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Jun 26 20:02:47 EDT 2001


Roman Suzi <rnd at onego.ru>:

> [=] ... b.width   # is get
> b.width =         # is set
> del b.width       # is del

But currently the only way of customising those incurs
far too much overhead, particularly __setattr__ which
incurs overhead for *every* attribute assignment, not
just the ones you want to trap.

I believe Guido has a scheme afoot to allow get/set
methods to be defined for individual attributes, so
this may change one day.

But even then, I'd be reluctant to use it for everything.
In my GUI library, many of the things I'm using set_xxx
methods for have noticeable side effects, e.g. changing
the size of a widget causes things to move on the
screen. Hiding functionality like that behind what looks
like an ordinary attribute assignment seems like a bad
idea to me.

So, I stand by my opinion that having methods called
set_xxx is quite Pythonic, on the grounds that explicit
is better than implicit.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+




More information about the Python-list mailing list