[Tutor] Accessing class attributes: use methods only?

Chris Lasher chris.lasher at gmail.com
Tue Feb 20 00:31:56 CET 2007

Thanks very much for your responses, all. Just to clarify, yes, by
"through class methods" I actually meant "through methods of instances
of a class".

Now for more discussion: I'm confused. On the one hand we have Mr.
Kent Johnson's statement:

On 2/13/07, Kent Johnson <kent37 at tds.net> wrote:
> Python practice is to use direct attribute access. If you need to do
> something special when an attribute is read or set, you can create a
> property. This allows you to define methods to be called when the
> attribute is accessed, without having to change the client code.

So to paraphrase, he states it's the Python way to do:
my_instance = MyClass()
my_instance.x = 42

On the other hand, we have Mr. Alan Gauld, who states:

On 2/13/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
> Its generally good OOP practice to interact with object via messages.
> Its also good practice NOT to access an objects attributes directly
> (and that includes via get/set methods) A class should publish a
> set of operations. The attributes should be there to support
> those operations.

So to paraphrase, he states it's the right way, regardless of language, to do:
my_instance = MyClass()

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?


More information about the Tutor mailing list