[Tutor] Accessing class attributes: use methods only?

Python python at venix.com
Tue Feb 20 01:04:03 CET 2007


On Mon, 2007-02-19 at 18:31 -0500, Chris Lasher wrote:
> I'm used to just setting and getting attributes straight, which would
> be Pythonic according to Kent and yet also outright wrong according to
> Alan and academic papers. So is direct access actually not Pythonic,
> or is it Pythonic and Pythonistas spit in the face of Demeter and her
> lovely laws?

Kent and Alan can speak quite eloquently for themselves, but just to
provide a more immediate answer, I expect they mostly agree with each
other.

The issue isn't whether you code:
        my.x = 42
        or
        my.setx = 42

Alan is saying you should not generally be twiddling attributes in an
object.

Kent is suggesting that if you do decide to twiddle attributes in
Python, just do it directly.  If later on you decide you need some
method logic to control the attribute twiddling, you can use
        property
to invoke methods when directly accessing the attribute.  I do not think
there is anything to be gained in Python by expecting your object
interface to depend on the use of get/set methods.

-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list