[Tutor] Re: when to use properties?

Ryan Davis ryan at acceleration.net
Wed Apr 13 15:35:13 CEST 2005

I agree with Andrei:

Use an instance variable until you need it to do something special, and then convert it to a property.

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf Of Andrei
Sent: Wednesday, April 13, 2005 2:41 AM
To: tutor at python.org
Subject: [Tutor] Re: when to use properties?

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.



Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list