Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.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; 'function,': 0.07; 'iterate': 0.09; 'nicely': 0.09; 'statement.': 0.09; 'successive': 0.09; 'am,': 0.12; 'skip:[ 20': 0.12; 'debugging': 0.13; 'def': 0.15; '(lambda': 0.16; '52,': 0.16; 'complicated,': 0.16; 'expression,': 0.16; 'expression.': 0.16; 'expressions,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'helps!': 0.16; 'instance:': 0.16; 'lambda': 0.16; 'read:': 0.16; 'mon,': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'defined': 0.19; 'header:In-Reply-To:1': 0.22; 'sep': 0.23; 'code': 0.25; 'keeps': 0.26; 'function': 0.27; 'separate': 0.28; 'needed,': 0.28; 'message-id:@mail.gmail.com': 0.29; 'times.': 0.30; 'conditional': 0.30; 'elsewhere': 0.30; 'logic': 0.30; 'chris': 0.32; 'list': 0.32; 'expression': 0.32; 'value.': 0.32; "isn't": 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; 'named': 0.33; 'that,': 0.33; '...': 0.34; 'see,': 0.34; 'similar': 0.35; 'received:209.85.161': 0.35; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'sense': 0.39; "i'd": 0.40; "it's": 0.40; 'where': 0.40; 'more': 0.60; 'your': 0.61; 'hope': 0.61; 'back': 0.62; 'here': 0.65; 'as:': 0.70; 'become': 0.71 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=SO400aS2TGvpSmtSeQv9EV3T62T2EX6Hf+6WG2fOyu8=; b=wWqAK9UJoitioDeIzVhEZZBlQYRYiTeTbO1bJFdJ6yh1hp2oBHZj9lENar847mbtMG YYLMTpW2E3i/vSFbdQXoqPO2t/oadWWHmHaim5dXVt3x9SJl/+dVYTpae115BIto/GmK qlzWLpK6rcdk8a6BXpWY/pT8RpKXBQIIWkkDQ= MIME-Version: 1.0 In-Reply-To: <4E6D015A.3070500@stoneleaf.us> References: <678b89fd-82f4-4080-8b36-e4d0ba30cdbf@o9g2000vbo.googlegroups.com> <4E6D015A.3070500@stoneleaf.us> Date: Mon, 12 Sep 2011 11:03:24 +1000 Subject: Re: Doctest failing From: Chris Angelico To: python-list@python.org 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315789407 news.xs4all.nl 2532 [2001:888:2000:d::a6]:42576 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13153 On Mon, Sep 12, 2011 at 4:43 AM, Ethan Furman wrote: > Chris Angelico wrote: >> >> And I'd do this with a lambda, but that's just me. Of course, if your >> logic is more complicated, it makes more sense to keep it in a named >> function, but a single conditional call can fit nicely into a lambda. > > Lambdas are great when needed, but if don't *need* it, and you have more > than a few, debugging can be a nightmare... "Okay, so this is function > ... and that is function ... and over here we also have > function ... ARGH!" Yeah, they can be like the Bruces sketch at times. A lambda is basically a function defined in an expression. For instance: def add_one(x): return x+1 is (practically) the same as: add_one = lambda x: x+1 In each case, you can call that function and will get back a value. The advantage of lambdas is that, in a list comprehension or map call, the code is right there instead of being elsewhere in a def statement. But as you can see, they quickly become hard to read: [j+2 for i in [[1,2,3],[4,5,6],[7,8,9]] for j in (lambda x: [q+10 for q in x])(i)] Their main advantage isn't in list comps, where you can already use arbitrary expressions, but in calls that require a function as an argument. The map() function is very similar to a generator expression, but it can iterate over multiple iterables at once: >>> list(map(lambda x,y: x+y,[1,2,3],[40,50,60])) [41, 52, 63] Note how the lambda keeps the code right there, whereas a def would separate it out. It's obvious from this one expression that it's adding successive corresponding pairs. Hope that helps! ChrisA