[Python-ideas] Multiple arguments for decorators

Emanuel Barry vgr255 at live.ca
Mon Nov 30 20:07:29 EST 2015


An idea that I had a couple of times was to have a syntactic way to define multiple arguments for a @decorator. This would be good if you want to, for example, define a property with a getter and a setter. I had a few ideas, but none of them seem to be "the obvious way to do it". I'm including them anyway, but I'd rather see if people are interested in the idea or if it's just something that's not worth it. I'll gladly write a PEP if it gains a lot of interest.
Method 1: Add parens around all arguments
class Foo:    def __init__(self):        self._x = 42    @property(    def x(self): # getter        return self._x    def x(self, value): # setter        self._x = value    def x(self): # deleter        del self._x    )
This one was my first idea, but with long methods you might very well miss the closing paren, or even wonder "how does the code even run?"
Good for small properties like this, bad with longer methods. -1 from me
Method 2: Specify how many arguments in the decorator
class Foo:    def __init__(self):        self._x = 42    @3:property    def x(self): # getter        return self._x    def x(self, value): # setter        self._x = value    def x(self): # deleter        del self._x
This one would get all three following methods defined (or should it get any value defined -- for example, a None in place of the setter there, would set the setter to None). Implementation-wise, I believe the order is kept while the class is first evaluated, and then often thrown away when it's inserted into the class' __dict__ (I could be wrong, though).
This has the advantage of not breaking code already written, as it cannot possibly conflict with an existing name. I'm neutral here.
Method 3: Specify arguments using the parameters' names
class Foo:    def __init__(self):        self._x = 42    @property    def x:fget(self):        return self._x    def x:fset(self, value):        self._x = value    def x:fdel(self):        del self._x
This has the advantage of being explicit (and the arguments can be swapped since they're named), but the disadvantage that some builtins don't accept kwargs (this would be a good occasion to fix that, though, but that is besides my point).
I've got mixed feelings on this one. I'm neutral as well here.
What do you guys think? Is this a good idea, or is it not?
Thank you for your time!-Emanuel 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151130/7a436aa0/attachment.html>


More information about the Python-ideas mailing list