Method chaining on decorator got SyntaxError

MRAB python at mrabarnett.plus.com
Thu Feb 17 00:49:30 CET 2011


On 16/02/2011 23:25, Makoto Kuwata wrote:
> 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.
>
You may want to read the discussion at:

https://groups.google.com/group/python-ideas/browse_thread/thread/1eebf486969c39a1/?hl=en



More information about the Python-list mailing list