Standard behaviour of a getSomething method

John Roth newsgroups at jhrothjr.com
Sat Jul 26 18:15:17 EDT 2003


"Stefan Schwarzer" <sschwarzer at sschwarzer.net> wrote in message
news:bfulr8$5mi$1 at online.de...
> Mike Rovner wrote:
> > Batista, Facundo wrote:
> >
> >> When I want to know about a attribute (e.g.: myAttrib) of an object, I
> >> should use the specific method (e.g.: getMyAttrib).
> >
> > Python don't enforce nor encourage attribute hiding,
> > so don't do it unless you have a good reason.
> > Plain access to attributes is simple:
> >     myObject.myAttribute
>
> I think, you should hide an attribute if it doesn't refer to the
> object's abstract interface but instead merely depends on the
> implementation. In that case, you shouldn't provide any means to
> access the attribute directly, be it via direct attribute access or
> accessor methods.

I agree with the general principle: implementation details shouldn't
be generally availible in the same way as the public API.

> Somewhat, I dislike the sometimes recommended __getattr__/__setattr__
> approach. I wouldn't like to have something like

That sounds more than a bit heavyhanded to me. The more
general practice is simply to name private attributes with a
leading underscore. Then you can verify that there aren't any
improper references by scanning the source for references to
underscored variables that don't start with "self._".

John Roth






More information about the Python-list mailing list