[Tutor] Accessing class attributes: use methods only?
dkuhlman at rexx.com
Wed Feb 14 18:27:45 CET 2007
On Tue, Feb 13, 2007 at 11:35:47PM -0500, Kent Johnson wrote:
> Bob Gailer wrote:
> > I really like the simplicity of a.b = 3. I groan when put in other
> > environments where a method call is required.
> > And Python has the magic method __setattr__ to intercept attribute
> > assignment for the times where some inspection / protection /
> > side-effect action is desired.
> The modern way to do this (since Python 2.2 I think) is to use
> properties. __setattr__ has other uses (for example for delegation) but
> it is too big a hammer for changing the behaviour of a single attribute.
NTSAAB (Not to start an argument but) ...
Some of us old school types feel that properties are non-Pythonic.
They are a way to write code that does something that it does not
look like that code is doing. It hides your intend. So, it is not
"Explicit is better than implicit."
- Tim Peters
In other words, properties are a way of writing code that appears
harmless, for example:
temp = weather.temperature
weather.temperature = temp
but making it do arbitrary, sneaky, gratuitous, and egregious things.
See the documentation on the *property* function at
However, some of you are probably saying to yourselves: "Well, of
course (we're sneaky; we're devious; ...). I mean we are
programmers, after all."
OK. OK. Maybe I had too much coffee this morning.
More information about the Tutor