+ 4

Identical id's

Why are id's of "i" identical? def f(): return [lambda: id(i) for i in range(5)] for i in f(): print(i())

7th Sep 2019, 4:52 PM
SergeiRom
3 odpowiedzi
+ 3
~ swim ~ I tried range(500) but id's are identical too. I am calling "id" function (i.e. id(i)) inside the lambda, so id() is not needed on the program last line.
7th Sep 2019, 5:21 PM
SergeiRom
+ 3
~ swim ~ I meant the same id printed with the code: def f(): return [lambda: id(i) for i in range(500)] for i in f(): print(i()) You wrote that I forgot id() call in the last line of my program, but id() in the last line is not necessary because it is already in the lambda body. "i" is not an int here, it is a lambda from lambda list returned by "f" function. I should have used "lmbd" name instead of "i" in the loop for better obviousness.
7th Sep 2019, 5:52 PM
SergeiRom
+ 3
~ swim ~ Yes, the code gives different id's. I had similar thoughts about closuring. But I'm confused about why just the last value is closured and not the first, for example.
7th Sep 2019, 7:06 PM
SergeiRom