Method chaining on decorator got SyntaxError

Makoto Kuwata kwa at kuwata-lab.com
Wed Feb 16 18:25:02 EST 2011


Hi,

I have a question about decorator.
I tried the following example and got Syntax Error.

    class deco(object):
        def __init__(self, name):
            self._name = name
        def foo(self, value):
            self._foo = value
            return self
        def __call__(self, func):
            func._deco = self
            return func

    ## ok
    @deco('aaa')
    def f1(): pass

    ## Syntax Error
    @deco('aaa').foo('bbb')  # SyntaxError: invalid syntax
    def f2(): pass

The above code shows that Python doesn't allow method chain
on decorator syntax.
Why does this limitation exist?
I want to chain methods as a certain DSL, just like:

    @recipe().product('*.html').ingreds('$(1).rst')
    def file_html(c):
        system(c%"rst2html.py $(ingred) > $(product)")

If you know the reason of the restriction, let me know it.

--
regards,
makoto kuwata



More information about the Python-list mailing list