Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Meta decorator with parameters, defined in explicit functions Date: Tue, 28 Jun 2016 09:26:30 +0200 Lines: 51 Message-ID: References: <85mvm5vrbw.fsf@benfinney.id.au> <87h9cddba1.fsf@handshake.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de /9nHenToxHxxAOOetSXdKwhdBtVi9E/9lct4cbty8Dyg== Cancel-Lock: sha1:TseOhBW5aHWQjvKefby1E0Cg/S4= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'essentially': 0.04; 'tries': 0.05; '*args,': 0.07; 'nasty': 0.07; 'func': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python.': 0.11; 'def': 0.13; 'argument': 0.15; 'copy-pasted': 0.16; 'definition.': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:parameters': 0.16; 'arguments': 0.22; 'decorator': 0.22; 'explicit': 0.22; 'function,': 0.22; 'bit': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'define': 0.27; 'defining': 0.27; 'function': 0.28; 'looks': 0.29; '**kwargs)': 0.29; 'clever': 0.29; 'does,': 0.29; 'allows': 0.30; 'that.': 0.30; 'code': 0.30; 'another': 0.32; 'problem': 0.33; 'common': 0.33; 'usually': 0.33; 'definition': 0.34; 'returning': 0.35; 'knowledge': 0.35; 'but': 0.36; 'there': 0.36; 'form,': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'signature': 0.37; 'charset:us-ascii': 0.37; 'itself': 0.38; 'does': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'avoid': 0.61; 'body': 0.61; 'real': 0.62; 'bases': 0.63; 'more': 0.63; 'natural': 0.67; '"decorator"': 0.84; '@decorator': 0.84; 'decorate': 0.84; 'decorator.': 0.84; 'decorator:': 0.84; 'lacks': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57b38b3e.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <87h9cddba1.fsf@handshake.de> X-Mailman-Original-References: <85mvm5vrbw.fsf@benfinney.id.au> Xref: csiph.com comp.lang.python:110665 Ben Finney writes: > I want an explicit replacement for a common decorator idiom. > > There is a clever one-line decorator that has been copy-pasted without > explanation in many code bases for many years:: > > decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda func: decorator(func, *args, **kwargs) > > My problem with this is precisely that it is clever: it explains nothing > about what it does, has many moving parts that are not named, it is > non-obvious and lacks expressiveness. I have been able to understand the purpose of the definition above - based solely on background knowledge about "decorator" and the definition. A decorator is a function which takes a function as argument and returns another function, the decorated function. In its simplest form, it is usually is used like: @decorator def f.... However, there is a more complex use: a decorator with arguments. It takes the form @decorator(...args...) def f... In this use, the decorator is not "decorator" itself but "decorator(...args...)". "decorator" itself is in fact a "meta" decorator taking arguments and returning the real decorator. The standard way to define such a meta decorator would be to have a local function definition in its body and return that. Locally defining functions and returning them looks a bit nasty in Python. You might want to avoid it. That's what the "decorator_with_args" above tries to facilitate. It allows you to define a function "decorator" with arguments "func, *args, **kw", decorate it with "decorator_with_args" and use this as a decorator with arguments ("*args, **kw). "decorator_with_args" essentially is a signature transform. It transforms a function "func, *args, **kw --> ..." into a function "*args, **kw --> func --> ..." *AND* it does this in a completely natural and understandable way.