attribute decorators

G

gert

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'
 
G

Gert Cuykens

sorry for repost i just found out the news group comp.lang.python is
the same as (e-mail address removed) :)
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top