Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'arguments': 0.07; 'caller': 0.07; 'subject:skip:c 10': 0.07; 'positional': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'applies': 0.15; "'b'": 0.16; "'c'": 0.16; 'c):': 0.16; 'funcs': 0.16; 'functools': 0.16; 'instead:': 0.16; 'lambda': 0.16; 'opposite': 0.16; 'wed,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'bit': 0.21; 'import': 0.21; '"",': 0.22; 'noted': 0.22; 'defined': 0.22; 'cheers,': 0.23; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'values': 0.26; '(most': 0.27; 'order.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'subject:list': 0.28; "d'aprano": 0.29; 'steven': 0.29; 'daniel': 0.30; 'keyword': 0.30; 'error': 0.30; 'file': 0.32; 'could': 0.32; '+0200,': 0.33; '11,': 0.33; 'traceback': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'built-in': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'bad': 0.37; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'notice': 0.39; 'header:Received:5': 0.40; 'jul': 0.65; 'clearer': 0.84; 'to:name:python': 0.84; 'either:': 0.91; 'subject:funny': 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=3n2HQoOxHMRf+wYJiMNsjPwxGHFN6OHBaNos2PkhRkw=; b=jrdbvya79ab/14HErNk73DAmCQqcUxeiu1LvoBb6CMYMIM/fUBqLYBG6K6KhT3Di0t vuVEdK4c4kIOJwSh9R1p9Z1VteNjlW4gcXMnTbosz5pio+Y1fbhnL4XEyEORb3DV67mq r3YNbvyJRBK34RN9Q1DG493bnvQxcGLDMloMlE9MgpQGKzDkCq8IjeX4PXyGirnLKNT2 4YS3LRrqCuTpfDwF8CnBJMExspKRos8fKZ84SsGYU9r2evaBSxW50C474W03151X2RgZ aWbwCluQuZrh3iOdsROQHR4tklf/CQyG5s0R0x7yff5WGxgG0EAQY3iTLJlONN2dA4Kz 8+oQ== MIME-Version: 1.0 In-Reply-To: <4ffe4bbb$0$1781$c3e8da3$76491128@news.astraweb.com> References: <4ffe4bbb$0$1781$c3e8da3$76491128@news.astraweb.com> From: Ian Kelly Date: Thu, 12 Jul 2012 10:53:25 -0600 Subject: Re: lambda in list comprehension acting funny To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342112044 news.xs4all.nl 6980 [2001:888:2000:d::a6]:60370 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25225 On Wed, Jul 11, 2012 at 9:59 PM, Steven D'Aprano wrote: > On Wed, 11 Jul 2012 08:41:57 +0200, Daniel Fetchinson wrote: > >> funcs = [ lambda x: x**i for i in range( 5 ) ] > > Here's another solution: > > from functools import partial > funcs = [partial(lambda i, x: x**i, i) for i in range(5)] > > > Notice that the arguments i and x are defined in the opposite order. > That's because partial only applies positional arguments from the left. > If there was a "right partial" that applies from the right, we could use > the built-in instead: > > funcs = [rpartial(pow, i) for i in range(5)] You know, partial does handle keyword arguments correctly: funcs = [partial(lambda x, i: x ** i, i=i) for i in range(5)] This might be bad practice though if there are other arguments to the right of the keywords that the eventual caller might want to specify, as those arguments would then effectively be keyword-only. The error message in that case is not particularly illuminating, either: >>> def f(a, b, c): ... return (a, b, c) ... >>> g = partial(f, b=42) >>> g(3, 14) Traceback (most recent call last): File "", line 1, in TypeError: f() got multiple values for keyword argument 'b' It would be a bit clearer if it instead noted that 'c' were keyword-only in the 'g' argspec. Cheers, Ian