0

I am a c++ guy, learning the lambda function in python and wanna know it inside out. did some seraches before posting here. anyway, this piece of code came up to me.

<1> i dont quite understand the purpose of lambda function here. r we trying to get a function template? If so, why dont we just set up 2 parameters in the function input?

<2> also, make_incrementor(42), at this moment is equivalent to return x+42, and x is the 0,1 in f(0) and f(1)?

<3> for f(0), does it not have the same effect as >>>f = make_incrementor(42)? for f(0), what are the values for x and n respectively?

any commments are welcome! thanks.

>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
5
  • 1
    this is a very basic example. make_incrementor returns a lambda, which is a function. It can be used in cases where you need a function, such as in the map function. Consider this: map(f, range(0,10)) will return you a list of numbers from 42 to 51. You need a function for map that takes only 1 parameter. Commented Aug 18, 2014 at 18:21
  • just added a follow up question 3 just now @njzk2 Commented Aug 18, 2014 at 18:27
  • 1
    Just for reference here's a lambda tutorial that I've found particularly useful Commented Aug 18, 2014 at 18:29
  • Keep in mind, a lambda expression simply creates a new function object. f = lamba x: x + n and def f(x): return x + n produce identical function objects bound to f. Commented Aug 18, 2014 at 18:32
  • 1
    f is a function such as f(x) = x+n, n being the parameter give to make_incrementor, i.e. 42, x being the parameter to f. so f = make_incrementor(42) gives you f(x) = x + 42, and then f(0) = 0 + 42 Commented Aug 18, 2014 at 18:33

2 Answers 2

1
  1. Yes, this is similar to a C++ int template. However, instead of at compile time (yes, Python (at least for CPython) is "compiled"), the function is created at run time. Why the lambda is used in this specific case is unclear, probably only for demonstration that functions can be returned from other functions rather than practical use. Sometimes, however, statements like this may be necessary if you need a function taking a specified number of arguments (e.g. for map, the function must take the same number of arguments as the number of iterables given to map) but the behaviour of the function should depend on other arguments.

  2. make_incrementor returns a function that adds n (here, 42) to any x passed to that function. In your case the x values you tried are 0 and `1``

  3. f = make_incrementor(42) sets f to a function that returns x + 42. f(0), however, returns 0 + 42, which is 42 - the returned types and values are both different, so the different expressions don't have the same effect.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that map is perfectly happy taking multiple arguments, eg: map(operator.add, [1, 2, 3], [4, 5, 6]) is [5, 7, 9] - the lambda in the example is used to purely fix one of those arguments in place... it's also sometimes possible to use functools.partial for this depending on argument ordering etc... eg: map(functools.partial(operator.add, 42), range(10))
1

The purpose is to show a toy lambda return. It lets you create a function with data baked in. I have used this less trivial example of a similar use.

def startsWithFunc(testString):
    return lambda x: x.find(testString) == 0

Then when I am parsing, I create some functions:

startsDesctription   = startsWithFunc("!Sample_description")
startMatrix          = startsWithFunc("!series_matrix_table_begin")

Then in code I use:

 while line:
    #.... other stuff

    if startsDesctription(line):
        #do description work

    if startMatrix(line):
        #do matrix start work

    #other stuff ... increment line ... etc

Still perhaps trival, but it shows creating general funcitons with data baked it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.