[Python-ideas] Multiple arguments for decorators

David Mertz mertz at gnosis.cx
Mon Nov 30 21:41:15 EST 2015


On Mon, Nov 30, 2015 at 6:14 PM, Chris Angelico <rosuav at gmail.com> wrote:

> def call(func):
>     def inner(cls):
>         return func(**{k:v for k,v in cls.__dict__.items() if not
> k.startswith('_')})
>     return inner
>
> class Foo:
>     def __init__(self):
>         self._x = 42
>     @call(property)
>     class x:
>         def fget(self):
>             return self._x
>         def fset(self, value):
>             self._x = value
>         def fdel(self):
>             del self._x
>

I think this looks perfectly nice, actually.  I was just trying to work out
almost the same thing but using a `def x()` rather than `class f` as the
nesting construct.  I think Chris' is better though.  I think I might want
to define something like:

make_property = call(property)

class Foo:
    def __init__(self):
        self._x = 42
    @make_property
    class x:
        def fget(self):
            return self._x
        def fset(self, value):
            self._x = value
        def fdel(self):
            del self._x



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151130/27ceff0e/attachment.html>


More information about the Python-ideas mailing list