Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #13153

Re: Doctest failing

References <678b89fd-82f4-4080-8b36-e4d0ba30cdbf@o9g2000vbo.googlegroups.com> <ZFIaq.3398$GZ.577@newsfe03.ams2> <CAPTjJmr2KzwuzXgkpho38+9i-+=fUeQ0WdQj=36wOFd_vjYOMQ@mail.gmail.com> <4E6D015A.3070500@stoneleaf.us>
Date 2011-09-12 11:03 +1000
Subject Re: Doctest failing
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1011.1315789407.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Sep 12, 2011 at 4:43 AM, Ethan Furman <ethan@stoneleaf.us> 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
> <lambda>... and that is function <lambda>... and over here we also have
> function <lambda>... 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-10 04:20 -0700
  Re: Doctest failing Mel <mwilson@the-wire.com> - 2011-09-10 07:43 -0400
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:36 -0700
  Re: Doctest failing Peter Otten <__peter__@web.de> - 2011-09-10 13:47 +0200
    Re: Doctest failing ting@thsu.org - 2011-09-10 19:12 -0700
      Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:42 -0700
  Re: Doctest failing Thomas Jollans <t@jollybox.de> - 2011-09-10 13:50 +0200
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:39 -0700
  Re: Doctest failing Alister Ware <alister.ware@ntlworld.com> - 2011-09-10 12:24 +0000
    Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-11 01:56 +1000
      Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:40 -0700
    Re: Doctest failing Ethan Furman <ethan@stoneleaf.us> - 2011-09-11 11:43 -0700
    Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-12 11:03 +1000
      Re: Doctest failing Ben Finney <ben+python@benfinney.id.au> - 2011-09-12 11:37 +1000
        Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-12 13:06 +1000
          Re: Doctest failing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-12 14:29 +1000
  Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-10 13:59 -0400
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 04:46 -0700
      Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-11 13:03 -0400
  Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-10 15:36 -0400
  Re: Doctest failing Peter Otten <__peter__@web.de> - 2011-09-10 22:49 +0200
  Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-10 16:25 -0700
  Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-10 23:18 -0700
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:43 -0700
      Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-11 12:37 -0700

csiph-web