Doctest failing

D

Dennis Lee Bieber

Too much destruction in this post man, and yeah I would not be able to
explain the code for my homework.

The whole plan in posting it... Something for independent study <G>
 
C

Chris Angelico

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
 
C

Chris Angelico

Those are only practically the same if you ignore the practical worth of
a function knowing the name it was defined with. The latter does not
have that, hence I don't see it as practically the same as the former.

I know, but in the context of explaining what "lambda" means, it's
practically the same. Lambdas can't (afaik) have docstrings either,
etc, etc, but in terms of defining lambda, the two are more-or-less
equivalent.

ChrisA
 
S

Steven D'Aprano

I know, but in the context of explaining what "lambda" means, it's
practically the same. Lambdas can't (afaik) have docstrings either,
etc, etc, but in terms of defining lambda, the two are more-or-less
equivalent.

Help on function add_one in module __main__:

add_one(x)
Return x+1



Lambdas are functions, no more, no less. The only difference is that the
*syntax* of the lambda statement only allows a single expression rather
that the full block of a def, and no name. But because it returns an
ordinary function object, you can post-process it just like any other
function.

Unfortunately, tracebacks ignore the function.__name__ and print the code
object name:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: cannot concatenate 'str' and 'int' objects


and the code object name is read-only:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: readonly attribute
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top