encapsulation?

Lloyd Goldwasser goldwasser at demog.berkeley.edu
Tue Aug 7 22:25:53 CEST 2001


As a recent convert to Python (I have C, Objective-C, and Scheme, among
others, in my past), I'm curious about its lack of encapsulation.  'Way
back when I was using Objective-C -- and many other OO languages seem to
have a similar perspective -- there was a lot of emphasis on the ability
to keep the contents of objects independent of outside influence except
via well-delineated mechanisms.  Security and reliability were cited as
major benefits of this approach: every object knew exactly what the
outside world was going to see and exactly how the outside world might
act to change its state.  Python provides only the private variable
name-mangling mechanism for "encapsulating" the contents of objects, and
it's weak enough that dir(ClassName) announces all of those "hidden"
contents to the world, at which point they can be altered at will.  I
wonder whether this approach, making encapsulation a matter of
convention rather than something that's provided by the language, is a
liability.  It would seem to make Python ineligible for use in projects
that need a more reliable separation between objects and outside
access.  How hard (or desirable) would it be to provide a stronger kind
of encapsulation in Python?

Lloyd


-- 
Lloyd Goldwasser                                Department of Demography
goldwasser at demog.berkeley.edu                   2232 Piedmont Ave. #2120
(510) 642-0525                                  University of California
fax: (510) 643-8558                             Berkeley, CA  94720-2120



More information about the Python-list mailing list