Python Forum
help on understanding this output
Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help on understanding this output
#2
Python uses "late binding" here. Lambda is defined with variable i, but actual value of i is looked up when that lambda is called. After for loop i is 2, so all lambdas return 2.

Common trick to avoid this is to use such variable as another parameter:

lambda x, i=i : x + i  # <- this binds i to actual value of i
In your case lambda i=i : i would work as "expected".
Reply


Messages In This Thread
help on understanding this output - by landlord1984 - Mar-08-2017, 08:05 PM
RE: help on understanding this output - by zivoni - Mar-08-2017, 08:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Common understanding of output processing with conditional statement neail 6 878 Sep-17-2023, 03:58 PM
Last Post: neail
  Help understanding RegEx logic/output pyNewbee 4 2,286 Nov-15-2020, 02:21 AM
Last Post: pyNewbee
  understanding output of bytes/raw data rootVIII 3 2,755 Aug-01-2019, 01:00 PM
Last Post: rootVIII
  Understanding "help()" output? Athenaeum 4 3,893 Sep-29-2017, 09:47 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020