Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deprecation of apply decorator

Tags:

python

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”?


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()
like image 618
Vadim P. Avatar asked Mar 04 '10 07:03

Vadim P.


People also ask

How do you deprecate in Python?

To mark a function or method as deprecated, wrap it in the deprecated() decorator. This does several things for you: The docstring of the wrapped function will have details appended to it from the arguments you set on deprecated() .

Can I use deprecated methods in Python?

Usage of a module may be 'deprecated', which means that it may be removed from a future Python release. The rationale for deprecating a module is also collected in this PEP. If the rationale turns out faulty, the module may become 'undeprecated'.

What Python mechanism is best suited for telling a user they are using a deprecated?

And you should be able to indicate deprecation when the function is imported from the module. Decorator would be a right tool for that.


2 Answers

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

The new Python 2.6 way is:

@property
def myattr(self):
    """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
like image 99
bobince Avatar answered Sep 28 '22 21:09

bobince


:) that is a clever user of apply, though i am not sure if there are any 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()
like image 43
Anurag Uniyal Avatar answered Sep 28 '22 21:09

Anurag Uniyal