[Tutor] designing POOP
Dave Kuhlman
dkuhlman at rexx.com
Sun Feb 10 18:28:05 CET 2008
On Sat, Feb 09, 2008 at 08:42:04PM -0500, Kent Johnson wrote:
> Alan Gauld wrote:
>
> >> def values(self):
> >> return (self.name, self.wealth, self.strenth)
> >>
> >> Or get rid of values() entirely and just refer to the attributes
> >
> > Nope, I don't like that as much since it encourages direct
> > access.
>
> Maybe we just need to agree to disagree here. I don't see how returning
> a tuple is better encapsulation than direct access to named attributes.
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.
- Dave
[some good arguments snipped]
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list