Today I had an interview, and I had a question that I answered incorrectly. 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 executed 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]. Can anyone explain? I'm so confused.
source
share