attribute decorators

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Jan 6 03:34:10 EST 2007


On Fri, 05 Jan 2007 20:34:54 -0800, gert wrote:

> Would it not be nice if you could assign decorators to attributes too ?
> for example
> 
> class C:
>   @staticattribute
>   data='hello'


You can. You just have to write it as:

class C:
    data = staticattribute('hello')

(Of course, writing the staticattribute function is a non-trivial problem.)


> or
> 
> class C:
>   @privateattribute
>   data='hello'

That would be written as 

class C:
    _data = 'hello'

or possibly 

class C:
    __data = 'hello' 

depending on whether you want private attributes to be by convention or by
name-mangling.


-- 
Steven.




More information about the Python-list mailing list