[Python-ideas] Multiple arguments for decorators

Cem Karan cfkaran2 at gmail.com
Mon Nov 30 20:54:45 EST 2015


On Nov 30, 2015, at 8:43 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Tue, Dec 1, 2015 at 12:07 PM, Emanuel Barry <vgr255 at live.ca> wrote:

<<SNIP>>

> But it's sounding here more like you're creating a block of code. And
> that, to my mind, suggests that it should be indented. Something like:
> 
> class Foo:
>    def __init__(self):
>        self._x = 42
>    with @property as x:
>        def fget(self):
>            return self._x
>        def fset(self, value):
>            self._x = value
>        def fdel(self):
>            del self._x
> 
> This groups the three functions, and their names would be available to
> use as keyword arguments. It would be rather different from current
> with-block semantics, though. Effectively, it'd be something like
> this:
> 
> 1) Evaluate the decorator itself (in this case, the simple name
> 'property'), but don't call it.
> 2) Create a new scope, nested inside the current scope. (Similar to a
> list comp.)
> 3) Execute the indented block in that scope.
> 4) Call the decorator, passing all names bound in this scope as
> keyword arguments.
> 5) Bind the return value of the decorator to the given name.

I like this; it really SHOULD be indented.  But I also agree that 'with' is probably not the best keyword here, it makes it a little difficult to quickly read and see what's going on.  Since this is for decorators, could we just drop 'with' altogether?  E.g.:

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

Thanks,
Cem Karan


More information about the Python-ideas mailing list