-1

Well I did try to read about Lambda functions but did not get across any link which explains few questions about its flow and the way it is handled by python interpretor or may be I could not understand it properly. I have few question, please can somebody clarify them for me. Here is the code :

def f1(n):
    print 'in f1, value is : ', n
    return lambda x: x+n

if __name__ == '__main__':
    f= f1(100)
    print f(1)
    print f(3)
    print f1(10)
    print f(5)
    print type(f1)

The output being :

in f1, value is :  100
101
103
in f1, value is :  10
<function <lambda> at 0x019C66B0>
105
<type 'function'>

My question is :

  • For f(1) and f(3) why print statement was not called and when we called f1(100) why lambda statement was not compiled.
  • What is the relation between x and n in function f1.
  • I thought f(5) will 15 ( 10 + 5)
  • Please explain print f1(10)
  • Also, please let me know what is lambda x: means here, is that x name of this block? please explain.

Thanks all.

David Zwicker
  • 23,581
  • 6
  • 62
  • 77
ramd
  • 347
  • 2
  • 12
  • Try to search first before asking any question. refer this link. http://stackoverflow.com/questions/1085875/what-is-this-lambda-everyone-keeps-speaking-of/1086347#1086347 – tailor_raj Jul 24 '13 at 11:59
  • @tailor_raj : Well thanks for the link but even after reading that I would have still asked my bit of question because I am looking for certain specific explanation ( python based) and also I DID SEARCH before posting this question!!! I was so expecting elements like you would definitely shout back about integrity of my question, there is always one in almost every single question!!! So I would say stop discouraging people from asking questions. – ramd Jul 24 '13 at 12:08
  • It is not about discouraging people. Because if you search, definitely you will get more then whatever you ask. – tailor_raj Jul 24 '13 at 12:21
  • "please let me know what is lambda x: means here". Start at the documentation: https://docs.python.org/3/reference/expressions.html#lambda and follow the links. – njzk2 Apr 03 '17 at 19:25

3 Answers3

2

First you should understand that everything in Python is an object. And functions do come in everything.

From your function f1(), you are actually returning a reference to a lambda function. Which you can assign in any variable.

For e.g.:

>>> f = lambda x: x   # Assign reference to lambda function to f.
# Now you can call lambda function using `f`  
>>> f(1)
1

Now, let's move ahead with your example. Your first statement is inside if is:

f = f1(100)

This assignment is equivalent to:

f = lambda x: x + 100  # Replace `n` with `100` in return statment in `f1`.

So, the next two call is pretty straighforward:

print f(1)  # returns `1 + 100` = 101 (Replace `x` with `1`.
print f(3)  # returns `3 + 100` = 103

So, now you get the relation between x and n. 'x' is replaced by the argument to f and 'n' is replaced by argument to f1.

Please explain print f1(10)

f1() returns a reference to a lambda function. And that is what it will print. The output is similar to the one which you get in the below snippet:

>>> def func():
        return 5

>>> func
<function func at 0x021F0F30>

Except that func is replaced with lambda, since f1 returns a lambda function only.

I thought f(5) will 15 ( 10 + 5)

You would have got that output, had you re-assigned the return value of f1 to f in the previous statement. But, since you have just printed the value of f(10), f is still binded to - lambda x: x + 100. So you get 105.

And then you print the type of f1, which is a function only:

print type(f1)
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Great explanation. I was exactly looking for this answer. brings lot of clarity on lambda function now. Thanks again. Just a quick one, so within my lambda body, n is like a variable passed onto lambda function by the covering function ( f1 in this case). – ramd Jul 24 '13 at 12:28
  • 1
    @ramd. Yeah, you can say that. – Rohit Jain Jul 24 '13 at 12:37
1

A lambda is pretty much another function. To use it, you have to call it too.

You return the actual lambda to f, not the whole function f1. Along with this, the lambda isn't called when you return it. That is why when you call f(1) and f(3), it doesn't actually run f1, but only the lambda.

Remember how I said lambdas are like functions? Well x is an argument, while n is the local variable n that you defined in f1.

f(5) != 15 because you did not actually use f1(10) anywhere. You only printed it. if you did f = f(10), then it would be 15.

print f1(10) prints the lambda function (because that is what is being returned). It doesn't call it, just prints it.

TerryA
  • 58,805
  • 11
  • 114
  • 143
1

A lambda is just a way of writing a function on one line.

Initially you may not see the point in them, but they can be useful for generating lots of slightly different functions, or for specifying functions you may only use once, or all other various instances.

To try and understand a little better, imagine the following code - slightly different from your own:

def f2(n):

    def g2(x):
        return x + n

    return g2

This does very much what your own code does, but by defining a function within the first function and then returning it. This essentially does the same thing as your code.

will
  • 10,260
  • 6
  • 46
  • 69