Python decorator syntax limitations

Jonathan S jonathan.slenders at gmail.com
Mon Jan 18 15:57:03 EST 2010


Thanks a lot, all of you! This was really helpful. (or at least give
me the inspiration I needed to finish it.)

I'm sure this is a use case where most other options are less readable
than the chain of methods in the decorator.
In this use case, I had a lot of Django views to which access
permissions had to be attached. Chaining the permissions was in my
opinion the easiest way to go but appeared not to work because Guido
had a gut feeling about that.
Using multiple decorators in the usual way (like every following wraps
the previous) does not work, because these decorators need to access
the same authentication class. (little hard to explain, but take from
me that I had a case where it didn't work.)
Placing all possible options in the constructor method of the
decorator like Steve Howell proposed would work but is ugly.

This is what I made. I'll upgrade my news_page decorator to work in
the following way. (Applying the news_page decorator to 'view', will
turn it into a class, but make  it callable buy wrapping the actual
view in __call__. So we can still apply the other methods to the view.

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


That is still rather ugly, because the 'lookup' option is appended
behind the view. I kept playing with the code and came to the
following:


@require_administrator
@do_lookup(News, 'news_id', 'news)
@news_page('template.html')
def view(request, group, news):
    ...
    pass


Where 'do_lookup' and 'require_administrator' passes the method, but
sets some parameters. So, 'do_lookup' can access class members of
news_page. It would look like:

def do_lookup(*lookup_options):
    def set_options(news_page_view):
        news_page_view.lookup(*lookup_options)
        return news_page_view
    return set_options

def require_administrator(news_page_view):
    news_page_view.require_administrator()
    return news_page_view


Maybe, I'll join the discussion later on, when I have time to read all
the conversations and write down good arguments.
But, honestly, I'm satisfied with the last result.
Have a nice day!



More information about the Python-list mailing list