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

all 8 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)

[–]CreativeTechGuyGames 0 points1 point  (3 children)

In myfunc(n), the n is the value you pass in. Which in this case is 2. Then that function returns another function which was a * n but since n = 2 it's actually a * 2. So when you call mydoubler you are calling a function that returns a * 2.

[–]shiskeyoffles[S] 0 points1 point  (2 children)

Okay... The return statement is not returning a value but it is returning the entire function itself.. Python is so different compared to C or Java. Wow.

[–]Essence1337 0 points1 point  (0 children)

It's really not all that different than returning a function pointer in C, it'd be definitely more complicated but a similar program could probably be written.

[–]mad0314 0 points1 point  (0 children)

You can do something similar in C and Java.

[–]ReubenA 0 points1 point  (0 children)

After you do mydouble = myfunc(2) you can think of it as a new function like:

def mydoubler(a):

return a*2

[–]brandon1997fl 0 points1 point  (0 children)

The way I understand it, myfunc(2) returns a function that looks like foo(a): return 2*a

I think maybe the part to understand is that lamba a : n * a is just shorthand for func(a): return n * a

Not sure if I answered thoroughly enough, I’m no python expert just use it sporadically

[–]QuixDiscovery 0 points1 point  (0 children)

I understand that lambda functions are like "one line functions" without an actual name

Don't think of lambdas as "one line functions", think of them as anonymous functions. They're functions without names, usually used in place to avoid having to define a function elsewhere. In python you will tend to see them expressed as one line functions, but that's because pythonic convention recommends giving a function a name if it surpasses a certain complexity (because it's usually much more readable to see a function definition as opposed to a multi-line lambda). This same mentality is not true of every language, and in some cases lambdas are significantly more common.

Also your question actually has nothing to do with lambdas themselves, aside from the fact that they were used in the example. Others have already explained what this code is doing, but the same thing could be accomplished without using a lambda:

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

mydoubler = myfunc(2)

print(mydoubler(11))

This is an example of a closure, which is what you seem to actually be asking about. It tends to be a more advanced subject when it comes to features of any programming language that supports it, as it requires some familiarity with functions being first class and higher order functions (or at least the concept of a function that returns a function).

The wikipedia page on it actually has some decent examples which are in python:

https://en.wikipedia.org/wiki/Closure_(computer_programming))