> Googling for "python is not java" may be a good start. Also, here are 2 > common C-style smells:
Ok, the first Google result (http://dirtsimple.org/2004/12/python-is-not-java.html) says this somewhere: "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'. (1) 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. (2) 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. (3) So, don't write getters and setters." For the record, I'm coming from Java, but I have some Python experience already. Now here's what I don't understand. What exactly is meant by (1)? The python library reference includes this as an example: class C(object): def __init__(self): self.__x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.") To me this seems contradictory. Why would one not want to do something that is included in the documentation? Or am I just confused? Does anybody have an idea how to put me in the right direction? And what does property mean anyway? The explanation in http://docs.python.org/lib/built-in-funcs.html is not enough for me. Can anybody point me to a more detailed documentation about this matter? (At first sight, the Python tutorial doesn't seem to describe this. At least it is not stated in the TOC, nor in chapter 9 which describes classes...) And then, by (2) I believe that what is meant is that once some client code uses public fields one can't make them private anymore because then the clients are broke. That's clear to me. But I don't understand (3). I don't know exactly what they mean. How would one want to change their mind? In what way? I've been thinking about it some time now, but I'm still kept in the dark. I would appreciate any explanation. I'd really like to get this, because when I use a language I want to use it "correctly", i.e. in the way it is intended, but right now I feel a bit lost :-( I'm not done with 'How To Ask Questions The Smart Way' yet, so please forgive any violations. (http://catb.org/~esr/faqs/smart-questions.html) Regards, antoine -- http://mail.python.org/mailman/listinfo/python-list