Creating properties with decorator like syntax

Discussion in 'Python' started by Will McGugan, Feb 17, 2005.

  1. Will McGugan

    Will McGugan Guest

    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
     
    Will McGugan, Feb 17, 2005
    #1
    1. Advertising

  2. Will McGugan

    Will McGugan Guest

    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
     
    Will McGugan, Feb 17, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandy Norton

    tweaking @decorator syntax

    Sandy Norton, Aug 4, 2004, in forum: Python
    Replies:
    14
    Views:
    494
    Dave Brueck
    Aug 6, 2004
  2. Delaney, Timothy C (Timothy)

    RE: Decorator syntax (was Re: PEP 318 - PyFIT comments)

    Delaney, Timothy C (Timothy), Aug 5, 2004, in forum: Python
    Replies:
    16
    Views:
    570
    =?ISO-8859-1?Q?I=F1igo?= Serna
    Aug 6, 2004
  3. Jeffrey Froman

    Decorator Syntax For Recursive Properties

    Jeffrey Froman, Apr 17, 2005, in forum: Python
    Replies:
    2
    Views:
    316
    Jeffrey Froman
    Apr 17, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  5. glomde
    Replies:
    5
    Views:
    540
    glomde
    Mar 29, 2007
Loading...

Share This Page