Method chaining on decorator got SyntaxError

Makoto Kuwata kwa at
Thu Feb 17 00:25:02 CET 2011


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
    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:

    def file_html(c):
        system(c%" $(ingred) > $(product)")

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

makoto kuwata

More information about the Python-list mailing list