deeps - 5 months ago 21

Python Question

I am new to python scripting.

I am confused how lambda interprets variables passed as in below example.

`def create_multipliers():`

return [lambda x : i * x for i in range(5)]

for multiplier in create_multipliers():

print multiplier(2),

returns 8 8 8 8 8

I see that lambda accepts only one argument (i.e 'x').

How does it interpret x and i in create_multipliers?

Also what does multiplier(2) mean?

Please help

Also with the below example

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

print make_incrementor(22)(33)

returned 55

How does the lambda/make_incrementor function decide which is 'x' and 'n'?

Answer

The first part of code creates a list of lambdas which each take single argument `x`

and multiplies it with `i`

. Note that each lambda is bound to variable `i`

not its' current value. Since the value of `i`

after the list comprehension is `4`

each lambda will return `x * 4`

:

```
>>> def create_multipliers():
... return [lambda x: i * x for i in range(5)]
...
>>> l = create_multipliers()
>>> l[0](1)
4
>>> l[4](1)
4
>>> l[4]('foobar')
'foobarfoobarfoobarfoobar'
```

The loop will then execute each lambda with parameter `2`

and print the results to same line. Since there are 5 lambdas and `4 * 2`

is 8 you get the output that you see. `,`

after the `print`

statement will result the output to be printed on the same line:

```
>>> for multiplier in l:
... print multiplier(2),
...
8 8 8 8 8
```

`make_incrementor`

works with same principle. It returns a lambda that takes single argument `x`

which is "decided" when lambda is called. It will then return `x + n`

where `n`

is the value passed to `make_incrementor`

:

```
>>> def make_incrementor(n):
... return lambda x: x + n
...
>>> inc = make_incrementor(2) # n is "decided" here
>>> inc(3) # and x here
5
```

**UPDATE** Example with nested functions:

```
>>> def make_incrementor(n):
... def nested(x):
... return x + n
... return nested
...
>>> inc = make_incrementor(2)
>>> inc(3)
5
```