Why does this not work ?
Python 3.11.6 (main, Oct 8 2023, 05:06:43) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
condition ={'RAPPORT': 1, 'COMPA': 0.815, 'varie': 'N_GRAIN'}
N_GRAIN, RAPPORT, COMPA, ACC, FREQ = 0.,0.,0.,0.,0.
print(locals()['RAPPORT'])
0.0
match = all(condition[key] == locals()[key] for key in condition if key != 'varie')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
KeyError: 'RAPPORT'
I do not understand why it gives me a KeyError, as 'RAPPORT' is in condition and in locals() !?
I reproduced it on python2 but with different key error. Here is the code :
Python 2.7.18 (default, Oct 15 2023, 16:43:11)
[GCC 11.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
condition ={'RAPPORT': 1, 'COMPA': 0.815, 'varie': 'N_GRAIN'}
N_GRAIN, RAPPORT, COMPA, ACC, FREQ = 0.,0.,0.,0.,0.
print(locals()['RAPPORT'])
0.0
match = all(condition[key] == locals()[key] for key in condition if key != 'varie')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
KeyError: 'COMPA'
Why does this not work ?
Python 3.11.6 (main, Oct 8 2023, 05:06:43) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
condition ={'RAPPORT': 1, 'COMPA': 0.815, 'varie': 'N_GRAIN'}
N_GRAIN, RAPPORT, COMPA, ACC, FREQ = 0.,0.,0.,0.,0.
print(locals()['RAPPORT'])
0.0
match = all(condition[key] == locals()[key] for key in condition if key != 'varie')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
KeyError: 'RAPPORT'
I do not understand why it gives me a KeyError, as 'RAPPORT' is in condition and in locals() !?
I reproduced it on python2 but with different key error. Here is the code :
Python 2.7.18 (default, Oct 15 2023, 16:43:11)
[GCC 11.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
condition ={'RAPPORT': 1, 'COMPA': 0.815, 'varie': 'N_GRAIN'}
N_GRAIN, RAPPORT, COMPA, ACC, FREQ = 0.,0.,0.,0.,0.
print(locals()['RAPPORT'])
0.0
match = all(condition[key] == locals()[key] for key in condition if key != 'varie')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
KeyError: 'COMPA'
Share
asked Mar 10 at 19:26
WillMPWillMP
111 bronze badge
2
|
1 Answer
Reset to default 4Your generator creates a new scope such that locals() now differs from the outer scope.
You can fix this by:
condition = {"RAPPORT": 1, "COMPA": 0.815, "varie": "N_GRAIN"}
N_GRAIN, RAPPORT, COMPA, ACC, FREQ = 0.0, 0.0, 0.0, 0.0, 0.0
locals_ = locals()
all(condition[key] == locals_[key] for key in condition.keys() if key != 'varie')
match = all(print(f"{locals().keys()=}") or condition[key] == locals()[key] for key in condition if key != 'varie')
– JonSG Commented Mar 10 at 19:47locals()
within a generator expression (or related comprehension constructs) creates a new scope. note, this behavior is changing in newer versions of Python that have received an optimization to how these constructs work underneath the hood: peps.python./pep-0709/#locals-includes-outer-variables although maybe not for generator expressions if I'm understanding that PEP correctly – juanpa.arrivillaga Commented Mar 10 at 20:32