[Tutor] Accessing class attributes: use methods only?

Kent Johnson kent37 at tds.net
Wed Feb 14 05:35:47 CET 2007


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.

Kent



More information about the Tutor mailing list