Reputation: 1848
Why there's no NameError raised for variable s
in below example?
from collections import defaultdict
s = defaultdict(lambda : len(s))
Upvotes: 1
Views: 37
Reputation: 522451
For the same reason that this works:
def foo():
print(bar)
bar = 'baz'
foo()
The lambda
only defines a function. By the time that function is called and it attempts to access s
, s
exists.
Upvotes: 4