Dave Kuhlman wrote: > Here is something relevant to this argument, although I'm not sure > that I agree with it: > > "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." > > extracted from: > http://dirtsimple.org/2004/12/python-is-not-java.html > > Maybe I'm "old school", but I still use getters and setters for > access from outside the class and direct access from within methods > in the class.
I agree with the quote and I think it represents mainstream thought in the Python community. AFAIK there is no advantage in Python to using an explicit getter or setter. Alan doesn't like direct attribute access *or* getters and setters, IIUC. He wants all access to the attributes of a class instance to be through higher-level methods. I think it is a good goal and technique to look for higher-level methods, and to create classes that are amenable to same, but that on a practical level attribute access works and I don't avoid it on principle. Kent _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
