0

Given a list A of any objects, I want to create another list B of lambda functions, where each B[i] () returns A[i]. For example,

B = [lambda: A[0], lambda: A[1], lambda: A[2], ...]

How can I do with for any arbitrary lengthed array A? Obviously, B = [lambda: element for element in A] does not work. What is a workaround for this?

crysoberil
  • 271
  • 3
  • 10
  • 2
    It is, in fact, a duplicate of late binding question. You can force early binding by something like this: `B = [lambda element=element: element for element in A]`. – Selcuk Apr 11 '18 at 00:12
  • Should `B[i]()` be the value of `A[i]` at the time `B` was created or at the time `B[i]` was called? – Blender Apr 11 '18 at 00:13

0 Answers0