Declarative properties

Artur Siekielski artur.siekielski at gmail.com
Thu Oct 11 09:04:53 EDT 2007


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.

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. And public
fields definition is mixed with private fields, which doesn't happen
when using properties.

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




More information about the Python-list mailing list