attribute decorators

Terry Reedy tjreedy at udel.edu
Fri Dec 22 21:33:02 EST 2006


"Gert Cuykens" <gert.cuykens at gmail.com> wrote in message 
news:ef60af090612221456v49f3c4abo7ac7e42a6c47aad1 at mail.gmail.com...
| would it not be nice if you could assign decorators to attributes too ?
| for example
|
| class C:
|    @staticattribute
|    data='hello'
|
| or
|
| class C:
|    @privateattribute
|    data='hello'

No.

@g
def f(): pass

is equivalent to and an abbreviation fo

def f(): pass
f = g(f)

The rationale for the abbreviation is
1. let the human reader know immediately that the function will get special 
treatment
2. (related) put the notation about the function at the top with other 
header stuff
3. avoid retyping really_long_function_names three times (and there are 
uses of Python where long, multicomponent names are sensible and useful).

But nothing is gained and something is lost by writing clear code like

data = staticattribute('hello') # or privateattribute('hello')

in two lines.

Terry Jan Reedy







More information about the Python-list mailing list