[Python-ideas] Multiple arguments for decorators
Emanuel Barry
vgr255 at live.ca
Mon Nov 30 21:43:14 EST 2015
> The proposal would be strengthened by more examples. Currently,
> @property can do something very similar to what your proposal offers,
> so this is only a small improvement.
While searching for more examples, it struck me that I wouldn't want to allow only methods to be passed, but I would want any value (for the name and/or docstring, mainly). For example, consider the following code:
class Foo: @attribute def bar(self): return 42 bar.__doc__ = "The meaning of life, the universe, and everything."
I would benefit from being able to pass in the doc at the same time, as well. That is a poor example though, here is a better one:
@total_decorate(handler=custom_handler, name="special_cased")class Foo: <...>
Or, as I do sometimes:
@total_decorate()class Foo: <...>
This would benefit from being able to make that a single call (given some changes to the code) and avoid the ugly parens, that I see quite often. This example could be extended to reprlib.recursive_repr too.
I must admit that I currently lack in examples, I'll try to provide more tomorrow. I took property because it's simple and everyone knows about it (pretty much).
> > I personally don't like this, the colon there looks weird to me. And you're
> > just ignoring the first method's name and passing it positional, unlike the
> > others which are named. I like the general idea you're bringing though, but
> > it could use a tweak or three imo.
>
> Agreed that the colon looks weird, but I don't know of any better way
> to spell it. This was just a half-way house to the thought that
> followed, though.
I'm not particularly attached to any syntax, all my suggestions are only that - suggestions.
> I don't think it can, because there's no way for a context manager to
> say "tell me about all name bindings in this block". The nearest I can
> think of is a nested class definition, which I can make work, but it's
> definitely ugly:
Well, you store all the names in the previous frame in __enter__, and then do your stuff in __exit__. It *is* hacky though.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151130/4e685a5f/attachment.html>
More information about the Python-ideas
mailing list