Method chaining on decorator got SyntaxError

Discussion in 'Python' started by Makoto Kuwata, Feb 16, 2011.

  1. 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
     
    Makoto Kuwata, Feb 16, 2011
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    30
    Views:
    7,868
    Raymond DeCampo
    Sep 6, 2005
  2. Dmytro Sheyko
    Replies:
    3
    Views:
    1,988
    Dmytro Sheyko
    Jan 19, 2006
  3. Steven Bethard

    Think you've got a new decorator syntax?

    Steven Bethard, Aug 16, 2004, in forum: Python
    Replies:
    1
    Views:
    281
    David Fraser
    Aug 19, 2004
  4. Ashwin Nanjappa

    Retaining *this in method chaining

    Ashwin Nanjappa, Jan 19, 2007, in forum: C++
    Replies:
    6
    Views:
    328
    Ashwin Nanjappa
    Feb 1, 2007
  5. glomde
    Replies:
    5
    Views:
    526
    glomde
    Mar 29, 2007
Loading...

Share This Page