Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'guido': 0.05; 'subject:Python': 0.06; 'plenty': 0.07; 'squares': 0.07; 'variables': 0.07; 'deprecated': 0.09; 'pep': 0.09; 'subject:language': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '(it': 0.16; '24,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'once.': 0.16; 'shortcut': 0.16; 'similarly,': 0.16; 'stdlib.': 0.16; 'subject: \n ': 0.16; 'wrote:': 0.18; 'hey': 0.18; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'either.': 0.24; 'rid': 0.24; 'skip:l 30': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'possibly': 0.26; 'suggested': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; "doesn't": 0.30; 'skip:g 30': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'breaking': 0.31; 'there.': 0.32; 'cases': 0.33; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'example,': 0.37; 'pm,': 0.38; 'does': 0.39; "couldn't": 0.39; 'sure': 0.39; 'space': 0.40; 'how': 0.40; 'ian': 0.60; 'then,': 0.60; 'truly': 0.60; "you're": 0.61; 'name': 0.63; 'more': 0.64; 'capable': 0.67; 'mar': 0.68; 'anything.': 0.68; 'nobody': 0.68; 'subject:this': 0.83; '(according': 0.84; 'clearer': 0.84; "it'd": 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=FrKxsZMSVcTfGTMjBFS8eK3T4eKV+hzFwnOz9L/g5JI=; b=tvEzgDF9dDmSmW3QHEerOfaiGJM/eeIoFCuWrNK07jBCTdq7EhmZY1MzH5sxCIz8hP m7Hi0zV+XTOGm4L9mExRtR13Z/XTNFhxwb7m1Q2hmI3akf31jnkKVlRk4OqRbql/OA7B mh4S0dmUKnMXWzzDrwv5uIbc+AZLoR9X1S2tYA4N4DUPuhDBHiWDPPk3wOagBVHNbnCT KLjZIKEz2hwEGqc792ntG4CEkj6Wo5CNoyyHutK4DdTBxP1whkimLUXleu0QgZHVhPF5 zR1kbJbs02TtjeSoys+9TsvwcGFv2DmWCyi/09GXPkbupsJK7Z1p7t1ieqAxkJduKD61 AGew== MIME-Version: 1.0 X-Received: by 10.66.102.39 with SMTP id fl7mr69520830pab.43.1395661779053; Mon, 24 Mar 2014 04:49:39 -0700 (PDT) In-Reply-To: <87bnwv2a5v.fsf@elektro.pacujo.net> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <10101874-2995-4acd-9851-989603f052e3@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> <87bnwv2a5v.fsf@elektro.pacujo.net> Date: Mon, 24 Mar 2014 22:49:38 +1100 Subject: Re: Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395661782 news.xs4all.nl 2954 [2001:888:2000:d::a6]:40887 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68872 On Mon, Mar 24, 2014 at 8:55 PM, Marko Rauhamaa wrote: > Ian Kelly : > >> If lambda were going to be deprecated and removed then it already >> would have happened in Python 3, because Guido tried to do precisely >> that. I'm not sure what the reasons were for keeping it in the end >> (according to PEP 3099 it was because nobody suggested a suitable >> replacement), but if he couldn't get rid of it then, he never will. > > You never *need* (Python's) lambda for anything. Inner functions are > more capable and almost always more readable. It doesn't hurt to have > lambda, but I don't find any use for it, either. They're often not more readable. A lot of people seem to equate "verbose" with "readable", possibly by faulty extrapolation from unreadably crunched code with one-letter variables and no line breaks. But which of these is truly more readable? squares = [] for n in range(30): squares.append(n * n) squares = [n * n for n in range(30)] Similarly, there are plenty of cases where a nameless function is MUCH clearer than breaking it out into a separate def and then using the name once. Do you name the function for what it does internally? def get_oneth_element_index(item): return item[1].index L.sort(key=get_oneth_element_index) Or for how you're using it? def keyfunc(item): return item[1].index L.sort(key=keyfunc) Or do you just shortcut the whole thing by inlining it? L.sort(key=lambda item:item[1].index) Hey look, that's weakref.py line 941 right there. (It happens to be the alphabetically last use of lambda in the stdlib. Made a good example, although it'd be more common to have a space after that colon.) ChrisA