tags:

views:

82

answers:

4

Hi! There was a beautiful way to organize class property in frame of one function, by using the apply decorator.

class Example(object):
    @apply
    def myattr():
        doc = """This is the doc string."""

        def fget(self):
            return self._half * 2

        def fset(self, value):
            self._half = value / 2

        def fdel(self):
            del self._half

        return property(**locals())

But now apply has been deprecated.

Is there any possibility to achieve such simplicity and readability for property, with new, came instead “extended call syntax”?

A: 

You can always write your own:

def apply(f, a):
    return f(*a)

However, I'm not quite sure I see the benefit of using apply as a decorator in this way. What's the use case?

Greg Hewgill
I want to place declarations of fget/fset methods in scope of one function. Is there any other solution, except apply decorator?
Vadim P.
+1  A: 

:) that is a clever user of apply, though i am not sure if there are ant pitfalls?

anyway you can do this

class Example(object):
    def myattr():
        doc = """This is the doc string."""

        def fget(self):
            return self._half * 2

        def fset(self, value):
            self._half = value / 2

        def fdel(self):
            del self._half

        return property(**locals())
    myattr = myattr()
Anurag Uniyal
+4  A: 

Is there any possibility to achieve such simplicity and readability for property

The new Python 2.6 way is:

@property
def myattr():
    """This is the doc string."""
    return self._half * 2

@myattr.setter
def myattr(self, value):
    self._half = value / 2

@myattr.deleter
def myattr(self):
    del self._half
bobince
I didn't know this and I like it very way. The 'apply' magic above was so unreadable I didn't quite know what the question was about.I knew @property and use it a lot, but .setter and .deleter are new to me and look great (self-documenting code) :)
Jacek Konieczny
A: 

My approach is same as Anurag’s, but, I don’t now witch one is better, please look:

def prop(f):

    return property(**f())

class A(object):

    @prop
    def myattr():

        def fget(self):
            return self._myattr

        def fset(self, value):
            self._myattr = value 

        return locals()
Vadim P.