Python decorator syntax limitations

Steve Howell showell30 at yahoo.com
Mon Jan 18 18:00:44 CET 2010


On Jan 18, 8:44 am, Jonathan S <jonathan.slend... at gmail.com> 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?


You might want to consider doing something like this:

    def lookup_view(page_class, template, class_, id_, label):
        def decorator(func):
            return page_class(template).lookup(class_, id_, label)
(func)
        return decorator

    @lookup_view(news_page, 'template.html', News, 'news_id', 'news')
    def view(...):
        pass




More information about the Python-list mailing list