Declarative properties

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 11 18:35:36 CEST 2007


On Thu, 11 Oct 2007 15:39:29 +0000, Artur Siekielski wrote:

> On Oct 11, 4:21 pm, "sjdevn... at yahoo.com" <sjdevn... at yahoo.com> wrote:
>> > 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.
> 
> Nice thing about properites is that they are defined in more declarative
> way - in class body. Attributes are "defined" in a piece of code (which
> can be long and can contain loops etc.) by assigning a value to 'self'.

The convention is to bind all attributes in `__init__()`, possibly to
`None` if the real value is not available at that time, and document at
least the public ones in the class' docstring.  Tools like `pylint` check
for attributes that are introduced in other methods than `__init__()` and
give a warning.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list