[issue43605] Issue of scopes unclear in documentation, or wrongly implemented

Cong Ma report at bugs.python.org
Wed Mar 24 11:25:01 EDT 2021


Cong Ma <m.cong at protonmail.ch> added the comment:

I think this is in the same class of behaviours as

```
def func(l):
    def get(i):
        return l[i]
    print(eval("(lambda x: get(x))(0)"))  # Call anonymous lambda with the constant 0 as argument
```
 
Calls like ``func(["spam"])`` will not "work", and ``NameError`` is raised.

In this case, inside the lambda's body the name "get" can't be resolved. For the lambda body, the name "get" is a nonlocal but there's no way to access a nonlocal in a lambda.

The comprehensions, like lambdas, are in their own nested scope.

----------
nosy: +congma

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43605>
_______________________________________


More information about the Python-bugs-list mailing list