Accessors in Python (getters and setters)

Diez B. Roggisch deets at nospam.web.de
Mon Jul 10 08:33:23 EDT 2006


mystilleef wrote:

> Hello,
> 
> What is the Pythonic way of implementing getters and setters. I've
> heard
> people say the use of accessors is not Pythonic. But why? And what is
> the alternative? I refrain from using them because they smell
> "Javaish."
> But now my code base is expanding and I'm beginning to appreciate the
> wisdom behind them. I welcome example code and illustrations.

Which wisdom do you mean? The wisdom that a language that has no property
mechanism and thus can't intercept setting and getting of instance members
needs a bulky convention called JAVA Beans, so that _all_ uses of
properties are tunneled through some code, even if only a few percent of
these actually need that?

Or the wisdom that strangling developers by putting access modifiers with
approx. a dozen different rules in place is an annoyance to adult
developers to say the least?

These are the reasons they are not pythonic. We can intercept property
access (see the property descriptor, http://pyref.infogami.com/property),
and we trust in developers being able to judge form themselves if messing
with internals of code is a good idea or not.

Regards,

Diez






More information about the Python-list mailing list