最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Locals() strange behaviour python? - Stack Overflow

programmeradmin4浏览0评论

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
  • In addition to @adon-bilivit's answer you can see for yourself with match = all(print(f"{locals().keys()=}") or condition[key] == locals()[key] for key in condition if key != 'varie') – JonSG Commented Mar 10 at 19:47
  • because locals() 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
Add a comment  | 

1 Answer 1

Reset to default 4

Your 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')
发布评论

评论列表(0)

  1. 暂无评论