The benefit of properties (was Re: PEP 8: on member variables vs. attributes)

Peter Hansen peter at engcorp.com
Fri Jan 17 15:14:59 CET 2003


John Roth wrote:
> Properties are simply a piece of syntactic sugar on top
> of accessor functions. As such, they provide the same
> information hiding benefits as accessors.
> 
> It's a nice piece of syntactic sugar, but it does not provide
> anything fundamental that can't be accomplished in some
> other way.
> 
> What it does provide is clarity and a level of encapsulation
> that facilitates maintenance.

They can also provide an obfuscation technique because they 
make implicit things that should sometimes be explicit.  I 
think this might be Yet Another Tool That can Easily be 
Abused in the hands of an unthinking programmer.

When I was leading a team doing lots of Delphi development,
several developers (who had used Delphi much longer than I)
insisted on using properties for just about everything.  No
accessor methods at all.  

I rebelled against this at the time, almost outlawing properties,
because it got to the point where I couldn't tell when something
was just setting a value, possibly with a check or clipping or
something applied, and when the code -- which looked like a simple
assignment -- was actually storing some data in a file, sending
a message to another thread, stopping the machine's motion, and 
then launching a Space Shuttle.

No doubt forbidding them was an overreaction, but at least it
let us start getting a handle on the complexity of the code.

I've been looking forward to having a chance to let them redeem
themselves when I start using them with Python... if they can.
Do they really enhance clarity and maintenance, or might they be
more _in the way_ of easy comprehension?

-Peter




More information about the Python-list mailing list