1

I am looking at T.Scholak's gist

I will reproduce with Jupyter

def side_effecty_id(x):
        print(x)
        return x

Let's consider

lambdas_0 = tuple((lambda x: x >= side_effecty_id(i)) for i in range(10))
tuple(l(5) for l in lambdas_0)

Result

9
9
9
9
9
9
9
9
9
9
(False, False, False, False, False, False, False, False, False, False)

While

lambdas_1 = tuple((lambda i: lambda x: x >= side_effecty_id(i))(i) for i in range(10))
tuple(l(5) for l in lambdas_1)

Gives

0
1
2
3
4
5
6
7
8
9
(True, True, True, True, True, True, False, False, False, False)

I regard this as very good example. Why do we need double lambdas? I have compared lambdas_0 and lambdas_1

<function __main__.<genexpr>.<lambda>(x)>

and

<function __main__.<genexpr>.<lambda>.<locals>.<lambda>(x)>,

What does <locals> denote?

Richard Rublev
  • 7,718
  • 16
  • 77
  • 121
  • `` refers to the local scope of a function. In this case, the lambda function. In other words, it means that the `lambda x` has been defined in a function. – Aran-Fey Aug 30 '18 at 06:17

0 Answers0