Encapsulation unpythonic?

Roy Smith roy at panix.com
Sun Sep 1 20:59:25 EDT 2013


In article <mailman.468.1378083075.19984.python-list at python.org>,
 Ethan Furman <ethan at stoneleaf.us> wrote:

> On 09/01/2013 02:54 PM, Tim Delaney wrote:
> > Roy Smith wrote:
> >>
> >> Nothing is accessible in Python except via getters and setters.  The
> >> only difference between Python and, say, C++ in this regard is that the
> >> Python compiler writes them for you most of the time and doesn't make
> >> you put ()'s at the end of the name
> >
> > I think Roy is referring to the fact that attribute access is implemented 
> > via __getattr__ / __getattribute__ /
> > __setattr__ / __delattr__. From one point of view, he's absolutely correct 
> > - nearly all attributes are accessed via
> > getters/setters in Python.
> 
> Seems to me there is a difference between an underlying generic protocol for 
> data manipulation and "Python writing them 
> [getters/setters] for you".

Why?  When I write "foo.bar", a bunch of generic code gets run which 
figures out what value to return.  If I don't like the generic behavior, 
I can write my own __getattrr__(), etc, and make it do whatever I want.

How is that any different from, in C++, if you don't write a default 
constructor, the compiler will write one for you.  If you don't like the 
generic behavior you get from that, you can write your own and make it 
do whatever you want.



More information about the Python-list mailing list