Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; 'essentially': 0.04; 'method.': 0.05; '*args,': 0.07; 'decorator': 0.07; 'nested': 0.07; 'parameter': 0.07; 'params': 0.07; 'wrapped': 0.07; 'wrapper': 0.07; 'python': 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; 'accepts': 0.09; 'function:': 0.09; 'ignoring': 0.09; 'maker': 0.09; 'path,': 0.09; 'sep': 0.09; 'terry': 0.09; 'tuple': 0.09; 'unpacking': 0.09; 'def': 0.10; 'subject:not': 0.11; 'passing': 0.15; '**kwds)': 0.16; '**kwds):': 0.16; 'fancy': 0.16; 'functools': 0.16; 'lambda': 0.16; 'nesting': 0.16; 'reedy': 0.16; 'simpler,': 0.16; 'skip:@ 20': 0.16; 'wrapped:': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'module': 0.19; 'define': 0.20; 'parameters': 0.20; 'bit': 0.21; 'import': 0.21; 'people,': 0.21; 'decorators': 0.22; 'explicit': 0.22; 'simpler': 0.22; 'cheers,': 0.23; 'least': 0.25; 'header:In-Reply- To:1': 0.25; 'skip:m 30': 0.26; 'first,': 0.27; 'skip:@ 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'all.': 0.28; 'words': 0.29; 'class': 0.29; 'this.': 0.29; 'call.': 0.30; 'fri,': 0.30; 'function': 0.30; 'utility': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'compared': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'does': 0.37; 'option': 0.37; 'level': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'most': 0.61; 'first': 0.61; 'is.': 0.62; 'worth': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'here': 0.65; 'difficulty': 0.65; 'you:': 0.75; 'gain': 0.79; 'actually,': 0.84; 'irrelevant': 0.84; 'pain': 0.84; 'to:name:python': 0.84; 'wrapper,': 0.84; 'approach.': 0.91; 'do:': 0.91; 'write:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=hI4hQGtqTL41wtBAZE7E+ABJjakT70jH+/o1kksLZN4=; b=KwsOQTFNRDoizabkLWagbUVserdBhmfwpcWgwxjBqsh7sNXTJIrMRxZpaGPBlIhFBT L8gTvOzd+fuS07cFWIN+4NJwPRnqZK1iE7QxXbV96eZd31q0KS1LzDdUbr7CQj4adDVu zA6HkIUQKhPsHBxAnh+/m6DMeTs9X4DupQEcy8+kSMLG3mypHh/PnmHEcQEWYUxXQbpr oB+8yxd3RZaTZw8NhsJCDM0NYrbaE8grPuY/K/Iauk0BGfNmHX4LVZ+uGdl8appRnnMu gISwk4ojFOpWJIuMfsi+R/YKjqMBZZni6LbsUVxQT6wXYPVd8LS+0FTWffzmMIjE17+y UlJA== MIME-Version: 1.0 In-Reply-To: References: <20120914021211.GA15642@cskk.homeip.net> From: Ian Kelly Date: Fri, 14 Sep 2012 15:16:47 -0600 Subject: Re: Decorators not worth the effort To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 96 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347657439 news.xs4all.nl 6873 [2001:888:2000:d::a6]:42591 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29190 On Fri, Sep 14, 2012 at 2:29 PM, Terry Reedy wrote: > For a simple, unparameterized wrapper, the difficulty is entirely in the > wrapper maker. It must define the final wrapper as a nested function and > return it*. It is irrelevant whether the wrapper maker is used with pre-def > decorator syntax or with an explicit post-def call. > > *I am here ignoring the option of a class with __call__ method. > > For a parameterized wrapper, using decorator syntax requires passing the > parameter(s) first and the function to be wrapped later. This requires > currying the wrapper maker with double nesting. The nesting order may seem > inside-out to some. For most people, this is extra work compared to writing > a wrapper that accepts the function and parameters together and only has a > single level of nesting. > > In other words > > def make_wrapper(func, param): > def wrapper(*args, **kwds): > for i in range(param): > func(*args, **kwds) > return wrapper > > def f(x): print(x) > f = make_wrapper(f, 2) > f('simple') > > # is simpler, at least for some people, than the following > # which does essentially the same thing. > > def make_outer(param): > def make_inner(func): > def wrapper(*args, **kwds): > for i in range(param): > func(*args, **kwds) > return wrapper > return make_inner > > @make_outer(2) > def f(x): print(x) > f('complex') > > Is the gain of not repeating the wrapped function name twice in the post-def > wrapping call, and the gain of knowing the function will be wrapped before > reading the def, worth the pain of currying the wrapper maker? If only there were a conceptually simpler way to do this. Actually, there is. I give you: metadecorators! First, the simple, non-parameterized case: from functools import partial def make_wrapper(wrapper): return lambda wrapped: partial(wrapper, wrapped) With that simple function buried in a utility module somewhere, we can do: @make_wrapper def simple_decorator(func, *args, **kwargs): do_stuff() result = func(*args, **kwargs) do_more_stuff() return result Which I think is certainly easier to understand than the nested functions approach. Parameterized decorators are not much more difficult this way. This function: def make_parameterized_wrapper(wrapper): return lambda *params: lambda wrapped: partial(wrapper, wrapped, params) enables us to write: @make_parameterized_wrapper def complex_decorator(func, (param1, param2, param3), *args, **kwargs): do_stuff(param1, param2) result = func(*args, **kwargs) do_more_stuff(param2, param3) return result And now we have a fancy parameterized decorator that again requires no thinking about nested functions at all. Sadly, that last bit of syntax will only work in Python 2; tuple parameter unpacking was removed in Python 3. It's not a complicated upgrade path, however: @make_parameterized_wrapper def complex_decorator(func, params, *args, **kwargs): (param1, param2, param3) = params do_stuff(param1, param2) result = func(*args, **kwargs) do_more_stuff(param2, param3) return result Cheers, Ian