[BUG] locals() not return same dict in multiple calls
See original GitHub issueDescribe the bug
CPython locals()
returns same dict
for multiple calls, and update dict content when locals()
called.
Cython creates new dict
on each locals()
call.
Code to reproduce the behaviour:
def cython_locals_test():
a = locals()
b = locals()
return a, b, a == b
Expected behaviour
Cython returns ({}, {'a': {}}, False)
a == b
should be True
CPython returns ({'a': {...}}, {'a': {...}}, True)
Environment
Windows 11 Python 3.10 Cython 3.0.0a11
Additional context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Subscripting locals() in a dict comprehension fails with KeyError
Because all comprehensions in Python 3 are implemented using a hidden function, calling locals doesn't return the values you're expecting it ...
Read more >PEP 558 – Defined semantics for locals()
Free variables are returned by locals() when it is called in function blocks, but not in class blocks. Note: The contents of this...
Read more >Is it reasonable to use dictionaries instead of arguments?
Re-use of variable names - the local variable a in translate() is not the same entity as the variable a in the script;...
Read more >Lua 5.1 Reference Manual
In this case, all returned values are thrown away. Function calls are explained in §2.5.8. 2.4.7 – Local Declarations. Local variables can be ......
Read more >Understanding Maps in Go - DigitalOcean
You can call the values of a map by referencing the related keys. ... Even though the key sammy was not in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note that exactly what
locals()
does inside function scopes is currently not well defined by the language spec. PEP 558 is currently being proposed to standardise a specific behaviour, which is actually to return a fresh dict each time like Cython does.I used locals() for monitoring variables in function and track variable assignment(declaration) by calling
locals()
, found that the old locals and new locals not the same dict. I fixed my usage by assign the locals() to my dict cache each time if I need to update values, so it works in my case.