[Tutor] Accessing class attributes: use methods only?

Terry Carroll carroll at tjc.com
Wed Feb 14 20:10:58 CET 2007


On Wed, 14 Feb 2007, Dave Kuhlman wrote:

> 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.
> 
> 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.

Using __setattr__ is at least as sneaky, though.

If the sneakiness bothers you, use a setter/getter method and lose that 
simplicity Bob refers to.



More information about the Tutor mailing list