How lambdas work?

2019-07-24 00:09发布

I'm learning python using the tutorial on the official python website and came across this example:

>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43

Where does x get it's value from? I'm not familiar with how lambda works, I understand anonymous functions just fine from javascript but this has me stumped. Anyone care to shed some light? I'd be grateful.

标签: python lambda
8条回答
淡お忘
2楼-- · 2019-07-24 00:20

x gets its value from the parameter used to call f. n gets its value from the parameter used to call make_incrementor.

查看更多
Anthone
3楼-- · 2019-07-24 00:22

make_incrementor is a function that returns a function:

def make_incrementor(n):
  return lambda x: x + n 

When we call make_incrementor, we bind the parameter 42 to n, and return the concrete function:

f = make_incrementor(42) # fill in n at this point and return a function

This function we will return will therefore look like:

lambda(x): x + 42

f is bound to the returned function, so f will conceptually look like:

def f(x):
   return x + 42

When we call f, we provide the value for x

f(1) # x == 1
f(2) # x == 2
查看更多
ゆ 、 Hurt°
4楼-- · 2019-07-24 00:23

Here's how I understand lambda:

When we write x2, we often confuse two distinct ideas. Consider:

  1. x2 has an odd number of factors when x is an integer.

  2. x2 is larger than x when x>1.

  3. x2 has derivative 2x.

  4. x2 has an inverse, namely sqrt(x), for x>0.

The first two statements are about the square of a particular but unspecified number. In those statements "x" represents an arbitrary single thing, and x2 represents a single related thing.

The third and fourth statement are about x2, the function. But this is not clearly communicated in 3. and 4. except by context and shared understanding of Calculus. We need a notational device to distinguish between x2 the single (but arbitrary) value, and x2 the function. The device also needs to communicate that it is a function of x. Therefore lambda is invented:

"lambda x.x2" is written to make this distinction and communicate "the function of x whose value at x is x2". The lambda operator takes a name (x) and an expression (x2) and returns a function. This has the same consequences as normal function definition, except that the function doesn't automatically receive a name.

查看更多
走好不送
5楼-- · 2019-07-24 00:34

Disclaimer: I have pretty much no Python background. This is going off my Scheme/lambda calculus knowledge.

make_incrementor defines a function to make functions. make_incrementor(42) returns a function with x bounded to the lambda, and n with a value of 42. When you call the function f, the argument of f replaces the bound variable.

查看更多
不美不萌又怎样
6楼-- · 2019-07-24 00:34

lambda brings some lambda calculus to Python. In essence, this is what's happening: normally in lambda calculus a statement would look something like this:

sum = add 3 4

This would pass 3 and 4 to the add function and store the result in sum. However, we could also write something along the lines of add 3. Now, since add expects two arguments, we now get an object (or function, if you will) expecting one argument. That function will then call add with 3 as its first argument and whatever we pass it as the second argument. We can now do this:

func = add 3
sum = func 4

This will be equivalent to the previous example. However, you can now use func whenever you want to add 3 to something. (Granted, this doesn't seem useful in this example, but in more complex situations it is).

All this is closely related to currying, something very central in most functional languages. If you're interested in lambda calculus and its similarity to regular mathematics, I highly recommend that you take a look at Haskell.

查看更多
淡お忘
7楼-- · 2019-07-24 00:35

First off, here's a translation into JavaScript:

function makeIncrementor(n) {
    return function (x) {return x + n}
}

A lambda expression consists of three parts.

  • The word lambda (In JavaScript, the word function)
  • A list of parameters (In JavaScript, the part in (...))
  • Some expression to evaluate (In JavaScript, the part in {...}, automatically returned)

The list of parameters is the part before the colon. These parameters are visible within the scope of the expression. Whatever the expression evaluates to is returned as the result of the function.

Unlike in JavaScript, a lambda can only contain a single expression; you can't make an anonymous multi-line function with lambda.

查看更多
登录 后发表回答