Yeah for naivety! In the pretty widely discussed article: "Python Is Not Java" http://dirtsimple.org/2004/12/python-is-not-java.html """ Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well. In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters. """ Guido's example for properties in the "classic" "Unifying types and classes in Python 2.2" Begins to shed some light for me on the Pythonic use case for properties - when something quite specific needs to be triggered by an access to or attempt to set what is otherwise a regular attribute - in this case a constraint on setting x. http://www.python.org/2.2.2/descrintro.html#property class C(object): def __init__(self): self.__x = 0 def getx(self): return self.__x def setx(self, x): if x < 0: x = 0 self.__x = x x = property(getx, setx) I'm quite OK with properties - with this narrowly defined use case in mind. Anyway -*I* feel like this discussion led me somewhere. Art
participants (1)
-
Arthur