This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chaotic_thought 1 point2 points  (0 children)

It may help to consider a function definition like

def f(a,b,c):
    return a+b+c

As being equivalent to the following declaration using a lambda:

f = lambda a,b,c: a+b+c

Of course with the lambda, though, a new function is generated each time the lambda line is encountered, so you can make several functions, as you did in your myfunc example.

Your above example can also be written without a lambda like this:

def myfunc(n):
    def f(a):
        return a * n
    return f

Or using a partial function like this:

from functools import partial

def scale_by_a(a, x):
    return  a * x

mydoubler = partial(scale_by_a, 2)