0

I would like, for convenience, to define several functions as "slices" of a given one, along the following lines:

def f(x, k):
    print(x, k)


gs = []
for k in range(2):
    gs.append(lambda x: f(x, k))

But now I get

gs[0]('a') == ('a', 1)
gs[1]('a') == ('a', 1)

I would like to get:

gs[0]('a') == ('a', 0)
gs[1]('a') == ('a', 1)

I understand why I get the previous thing, and wonder if there is some standard way of obtaining the latter; in some sense I want to "freeze" the immutable value of k upon declaration of the lambda function.

Sasha
  • 1

0 Answers0