I've found that a lot of the decorator syntax limitations can be worked around with an identity decorator:

i = lambda x: x
@i(M(b).decorator())
def wrapped_func():
    pass

kind of ugly, but it works


On Thu, Feb 20, 2014 at 1:31 PM, Franck Michea <franck.michea@gmail.com> wrote:
Hi there,

Today I hit a limitation of decorator syntax, and I was wondering if
maybe this limitation could be removed, or if they could allow more.

The grammar for decorators is[1]:

    decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE

But `dotted_name` doesn't allow to write things like the comment put in
the snippet attached, or this smaller snippet:

    @M(b).decorator()
    def wrapped_func():
        pass

Although it looks possible to me to add this syntax, I was wondering
if it had been discussed previously, and if I could see that
discussion.

All discussions I found were a lot older, arround the time decorators
were designed and the syntax was being choosen. I also read PEP306 and
PEP318.

This is not a blocking issue since you can do with a temporary
variable, but I was wondering what were your thoughts on this.

Thank you very much,

[1]: http://docs.python.org/3.3/reference/grammar.html

===== Snippet ==========
class foo_decorator:
    def __init__(self):
        pass

    def __call__(self, func):
        return func

class Foo:
    def test(self):
        return foo_decorator()

class Bar:
    def __init__(self):
        self._foo = Foo()

def M(val):
    return val._foo

b = Bar()

# SyntaxError: @M(b).test()
m = M(b)
@m.test()
def func():
    print('Hello World!')

func()
========================

--
Franck Michea - EPITA/LSE/GISTRE 2014
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/