today I had an interview and got a question that I answered wrong. Here's the question:
def gen():
return [lambda x: x + i for i in range(4)]
print([m(1) for m in gen()])
The result is [4, 4, 4, 4]. My answer was [1, 2, 3, 4]. I also ran the following code.
def gen():
return (lambda x: x + i for i in range(4))
print([m(1) for m in gen()])
The result is [1, 2, 3, 4]. Could anyone explain? I am so confused.