By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,277 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,277 IT Pros & Developers. It's quick & easy.

Creating properties with decorator like syntax

P: n/a
Hi,

Is there any way of making properties using a decorator? The current way
of creating a property seems inelegant.

Something like this imaginary snippit would be nice, IMHO.

class C(object):
@make_property
def x(self):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x

Regards,

Will McGugan

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Will McGugan wrote:
Hi,

Is there any way of making properties using a decorator? The current way
of creating a property seems inelegant.

Something like this imaginary snippit would be nice, IMHO.

class C(object):
@make_property
def x(self):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x


Posted too soon. Played with the above and got something which works.
def make_property( func ):
return property( *func() )

class C(object):

def __init__(self):
__x= 1

@make_property
def x():
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
return getx,setx,delx

c= C()
c.x= 5
n= c.x + 5

Good idea, or bad?

Will McGugan
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.