How to write Smart Python programs?

Roy Smith roy at panix.com
Wed Oct 11 15:51:17 CEST 2006


Antoine De Groote <antoine at vo.lu> wrote:
> 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.

This is a good example of an anti-pattern.  The general thought process 
here is:

1) "I need the ability to control access to an object's internal state".  
This is fine.  Decoupling of interface from implementation is, in general, 
a good thing, and one of the guiding principles of OOD.

2) "In the language I'm using, the only way to do that is to write getter 
and setter functions".  Fair enough.  You gotta use the tools you have 
available to you.

3) "Getters and setters are an essential part of OOD".  Here's where things 
go wrong.  Just like one must learn to decouple interface from 
implementation in your programs, so must one learn to decouple them in your 
program design.  Getters and setters are an language implementation detail.  
There's nothing magic about adding the word "get" to the beginning of a 
variable name and putting "()" after it.  That's just how you spell 
"property" in C++ or java.

It used to be said, "You can write Fortran in any language".  The 
implication there was that a literal line-by-line translation 
(transliteration?) of a Fortran program into some other language usually 
resulted in a mess.  I think we can generalize that to "You can write any 
language in any other language".  Every language has a natural way of doing 
things.  When you try to bring the old idioms and design processes from one 
language over to another, it usually doesn't work.

Once, years ago, I was working in a lisp environment for maybe a year.  It 
took me a little while to learn the syntax and basic function set, but I 
never really "got" it.  I was writing C programs in lisp.  The same thing 
happens when I try to speak Spanish.  Even when I used Spanish a fair bit, 
I never really thought in Spanish.  I thought in English and translated.  
That doesn't work very well.



More information about the Python-list mailing list