Declarative properties

sjdevnull at yahoo.com sjdevnull at yahoo.com
Thu Oct 11 10:21:31 EDT 2007


Artur Siekielski wrote:
> On Oct 11, 2:27 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> > But why?  Default getters and setters are unnecessary and if you need
> > something other than the default you need to write it anyway more
> > explicitly.
>
> I see some problems with your approach:
>
> 1. If I use instance field 'name' which is accessed directly by other
> classes,
> and later I decide to implement nonstandard getter, I must refactor
> 'Person' class
> and in some places change 'name' to '_name' (assuming this is now the
> field's name).
> The problem is that I cannot automatically change 'name' to '_name'
> everywhere, because
> in some places I want public property value (eg. validated and
> formatted), and in other
> places raw property value.

In practice, it turns out to be a lot less work to deal with that
occasionally than to always deal with lugging around internal
attributes and external properties when they're really not needed.  By
writing everything as properties up front you're just creating more
work for yourself, and it's generally considered bad practice in
Python to have default getters/setters (whether part of a property or
not).

> 2. Properties define (a part of) public interface of a class. When
> using fields for public
> access, you must tell this explicitly in documentation, or use name
> convention.

Property vs. attribute doesn't make any difference here: both of them
are public, and part of the external interface, unless they're named
with a leading underscore.

Making something a property doesn't make it any more or less a part of
the public interface.

> 3. Using properties as first-class objects gives possibilities to use
> declarative approaches
> for constructing them.

This doesn't make any sense.  Properties and attributes are both
objects in python.




More information about the Python-list mailing list