Glyph on Nostr: This is possibly my least (most?) favorite #PythonOddity : ``` def loop(): for number ...
This is possibly my least (most?) favorite #PythonOddity :
```
def loop():
for number in range(10):
def closure():
return number
yield closure
eagerly = [each() for each in loop()]
lazily = [each() for each in list(loop())]
```
Understanding why `eagerly` and `lazily` differ is *crucial* to understanding how scopes work in Python, and illustrates a weakness in the otherwise pretty great “you don’t have to declare variables” structure of the language.
```
def loop():
for number in range(10):
def closure():
return number
yield closure
eagerly = [each() for each in loop()]
lazily = [each() for each in list(loop())]
```
Understanding why `eagerly` and `lazily` differ is *crucial* to understanding how scopes work in Python, and illustrates a weakness in the otherwise pretty great “you don’t have to declare variables” structure of the language.