3 ms·
A lambda is no different in scoping rules than a regular function def. If you put a def in a loop, you'll find the same behavior. Closures are only created for
by xapata 8y ago
A lambda is no different in scoping rules than a regular function def. If you put a def in a loop, you'll find the same behavior.
Closures are only created for functions defined inside other functions. Thus the term non-local. They're variables that are neither local nor global.
Don't call it broken when it works as intended.
- _sdegutis 8y ago“Works as intended” but also “unintuitive to most users” is still broken. Disclaimer: I have hated python ever since falling out of love with it 8 years ago for reasons just like this one so I’m definitely biased.
- xapata 8y agoSince programming is a rapidly growing field and Python is the first language for many, I suspect that Python's scoping rules are in fact intuitive to most of its users. The ones who stumble are those with intuition built by some other language.
- _sdegutis 8y agoI'd argue that's the opposite. Python was the first dynamic language I learned. The scoping rules in Python didn't bother me because I didn't know any better, not because they're intuitive or well designed.
- xapata 8y agoFair anecdote. I'll offer that in my experience teaching hundreds of students, most don't have trouble with the cascade from locals to globals to builtins, and quickly grasp that globals are looked up for each call. That closures work at all confuses the ones who are thinking carefully about local scope. I may have a biased sample of students, but I think it's decent evidence.