[Tutor] Classes, Object and Encapsulation

alan.gauld@bt.com alan.gauld@bt.com
Wed Mar 26 12:43:13 2003


> I'm, fairly new to Python and I understand that by default=20
> object attributes are public=20

Yes. Until C++ came along most OO languages either made all=20
attributes public(Object Pascal, Lisp variants etc) or all=20
private (Smalltalk etc). C++ put the decision in the hands=20
of the programmer and most other languages since have=20
followed suit.

Python takes the all public by default line and has gradually=20
added protection mechanisms over time. In practice most Python=20
programmers are happy to let good manners and convention=20
control attribute access.

Folks moving to Python from Jaba and C++ seem to get more=20
paranoid about things and want to use the protection mechanisms.

[ BTW you can bypass C++ protection fairly easily by doing
#define private public
#define protected public
#include <mySecretClass.h>
if you really feel mean.... You can also bypass it with some=20
pointer magic by derefencing the VMT and doing pointer=20
arithmetic... Not that you'd ever want to of course!]

> - PLUS you could always write you own "get" and "set" methods

That doesn't really provide protection unless you've also done=20
at least some of the other stuff you mentioned.

> So, as a Python beginner, what's the best way to protect my object's=20
> attributes?

As a beginner don't bother. Concentrate on good programming=20
discipline and learning how to use the language including OOP=20
effectively. Leave paranoia to the C++/Java guys.

> Are "get" and "set" methods pass=E9 in Python? =20

Nope, they are just a bad thing IMHO.
If you can't think of a better name for a method that getXXX then=20
its likely you don't need the method but need to rethink the design.
What responbsibility requires one object to know so much about=20
the internal data of another object? Why can't the owning object do=20
whatever you were about to do for you? Its called the Law of =
Demeter....


> If you use them, do you write them for every attribute? =20

I usae them very rarely and only write them for attributes that really=20
must be shared between objects. And thats still probably bad OO design=20
at work...

> instead, should you just use properties and let everyone access=20
> all of your object's attributes through dot notation?

If you are building a component for general distribution and reuse=20
there is a case to be made for using properties. Also if you are using=20
the object in a polymoprphic environment where there is a mix of=20
objects with public access and objects with protected access(for=20
some reason - say you are adding a new object to legacy code)
Otherwise I don't see a great deal of advbantage in using=20
properties - a lot of extra work for minimal gain.

> Finally, how do people in the "real" programming world deal=20
> with objects and  attributes? =20

Depends on the environment. In Lisp or Object Pascal I still tend=20
to leave everything exposed(even though those languages have added=20
protection mechanisms too) I can honestly say I have never yet been=20
burned badly by someone accessing data directly - if the interface=20
is rich enough there should be no need! If it isn't the easier=20
answer is usually to create a subclass which has the missing methods!


> because the Python code I've seen seems NOT to take=20
> precautions with object attributes and just leaves them public.

Yes, thats the Pythonic norm. Partly for historic reasons, partly=20
cause we're a trusting lot.

Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/