question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] locals() not return same dict in multiple calls

See original GitHub issue

Describe 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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
TeamSpen210commented, Sep 28, 2022

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.

0reactions
fanthoscommented, Sep 28, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found