List Info

Thread: Re: Accessing class attributes: use methods only?




Re: Accessing class attributes: use methods only?
country flaguser name
United States
2007-02-14 13:10:58
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.

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )