Method chaining on decorator got SyntaxError

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 17 11:12:51 CET 2011


alex23 <wuwei23 at gmail.com> wrote:

> Makoto Kuwata <k... at kuwata-lab.com> wrote:
>> I'm sad about this restriction because:
>>
>>     @recipe.product('*.html').ingreds('$(1).rst')
>>     def file_html(c):
>>         # do something
>>
>> is enough simple and more readable than:
>>
>>     @recipe.product('*.html')
>>     @recipe.ingreds('$(1).rst')
>>     def file_html(c):
>>         # do something
>>
>> But I'll follow the Python's philosophy.
> 
> Personally, I'd find this approach a lot clearer:
> 
> rst2html = recipe.product('*.html').ingred('$(1).rst')
> 
> @rst2html
> def file_html(c):
>    # etc
> 
> It allows for easier reuse of your decorators and it labels it in a
> way that I can understand from the name as opposed to having to
> decipher the decorator method chain to work out its intent.

Or another way would be to define `recipe` such that something like this 
works:

@recipe(product='*.html', ingreds='$(1).rst')
def file_html(c):
   ...




-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list