Python decorator syntax limitations

Peter Otten __peter__ at web.de
Mon Jan 18 18:46:08 CET 2010


Jonathan S wrote:

> Hi all,
> The following is what I want to do, but this results in a syntax
> error:
> 
> 
> @news_page('template.html').lookup(News, 'news_id', 'news')
> def view(request, group, news):
>     pass
> 
> 
> What does work is the equivalent old way of doing decorating:
> 
> 
> def view(request, group, news):
>     pass
> view = news_page('template.html').lookup(News, 'news_id', 'news')
> (view)
> 
> 
> 
> Any suggestions? I have my reasons for doing this, (news_page is a
> class, and __call__ is used to wrap the template.)
> I'm sure this is a limitation in the syntax, but would parenthesis
> somewhere help?

@apply(lambda:news_page('template.html').lookup(News, 'news_id', 'news'))
def view(request, group, news):
    pass

But I'd go with a dedicated helper function.

Peter



More information about the Python-list mailing list