[Tutor] Re: when to use properties?

Andrei project5 at redrival.net
Wed Apr 13 08:40:49 CEST 2005


Marcus Goldfish <magoldfish <at> gmail.com> writes:

> Are there guidelines for when properties should be used vs. instance
> variables?  For example, it often seems more convenient to directly
> use instance variables instead of properties, like MyClass2 listed
> below.  Is one class more pythonic than the other?

In Python I tend to use methods directly, even though I think properties are
clearer and I actively avoid calling getters/setters directly in Delphi. I just
can't be bothered to write the extra line for some reason :). 

On the other hand, I never write getters/setters just for the sake of it. In
your example I would not have a self._value, but a self.value which is accessed
directly, since the getter and setter don't do anything special anyway. Now, if
I had such code and needed to add some processing before accessing the variable,
I might change it into a property and add the appropriate methods, so that the
interface remains the same.

Yours,

Andrei




More information about the Tutor mailing list