Method chaining on decorator got SyntaxError

M

Makoto Kuwata

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top