Talks

The chaotic locals() and how we fixed it

Saturday, May 17th, 2025 4:15 p.m.–4:45 p.m. in Hall C

Presented by

Tian Gao

Experience Level:

Some experience

Description

Historically, Python stored local variables in a dict, which was convenient and consistent. Users can use locals() to get the same dict Python is using for the local variables. As Python evolves, local variables were moved to the frame stack to improve performance, which made it invisible from Python code. However, locals() was kept because we still need a way to access some or all of the local variables in a frame.

This became chaotic when we try to change the local variables through the dict - which is a common requirement for something like a debugger. What could possibly go wrong? Is the update remembered? Could it work with exec()? Will a random read in your code bites you? How can we reliably update the value of a local variable in a debugger?

Well the short answer is - we can't, not until PEP 667.

This talk will be about how we fixed the chaos in PEP 667 and what's the modern way to deal with local variables in a debugger.

Search