I wonder how free variables in lambda functions work in Python

consider the following:

>>> a = [1, 2]
>>> b = [a, 3, 4, 5]
>>> del a
>>> b
[[1, 2], 3, 4, 5]

b works since there is still a reference to [1, 2].

however, consider the following:

>>> a = 1
>>> b = lambda x: a + x
>>> del a
>>> b(1)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
NameError: name 'a' is not defined

b breaks. so the free variables in lambda functions are only stored as names but not as references to the values?

用咗 Neat CSS 嚟整,呢度嘅所有文字係根據 CC BY-SA 4.0 嘅條款發佈。