Creating properties with decorator like syntax

W

Will McGugan

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
 
W

Will McGugan

Will said:
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
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top