How can I force update the Python locals() dictionary of a different stack frame?

marius

In Python 2 (not sure about 3), the locals dictionary only gets updated when you actually call locals(). So e.g.

l=locals()
x=2
l['x']

fails because l doesn't have the key "x" in it, but

l=locals()
x=2
locals()
l['x']

returns 2.

I'm looking for a way to force an update of the locals dictionary, but the trick is that I'm in a different stack frame. So e.g. I'm looking to do

l=locals()
x=2
force_update()
l['x']

and I need to write the force_update() function. I know that from said function I can get the parent frame via inspect.currentframe().f_back, and even the parent (non-updated) locals via inspect.currentframe().f_back.f_locals, but how can I force an update?

If this seems convoluted, my main goal is to write a function which is shorthand for "{some} string".format(**dict(globals(),**locals())) so I don't have to type that out each time, and can instead do fmt("{some} string"). Doing so I run into the issue above.

Edit: With Martjin answer below, below is essentially the solution I was looking for. One could play around with exactly how they get the stack frame of the callee, here I do it via partial.

from functools import partial
from inspect import currentframe

fmt = partial(lambda s,f: s.format(**dict(globals(),**f.f_locals)),f=currentframe())
x=2
print fmt("{x}") #prints "2"
Martijn Pieters

Simply accessing f_locals on a frame object triggers the copy, so using inspect.currentframe().f_back.f_locals is enough.

See the frame_getlocals() function in the frameobject.c implementation:

static PyObject *
frame_getlocals(PyFrameObject *f, void *closure)
{
    PyFrame_FastToLocals(f);
    Py_INCREF(f->f_locals);
    return f->f_locals;
}

PyFrame_FastToLocals is the function used to copy the data from the interal array tracking locals values to a dictionary. frame_getlocals is used to implement the frame.f_locals descriptor (a property); see the frame_getsetlist definition.

The PyFrame_FastToLocalsWithError function used above is exactly what locals() uses to produce the same dictionary (by wrapping the PyEval_GetLocals function).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I combine each value of different keys of a dictionary to outcome a data frame in Python?

How can I create an Exception in Python minus the last stack frame?

How do I force a CloudFormation stack to update when the parameter is updated?

How to update a dictionary with different list elements in Python

How can I update an installed stack package?

How can I update values in this dictionary?

how can I update a dictionary in Ansible

How can I force Plotly animation to start by the last frame?

How can i convert a python dictionary with tuple keys to a pandas multi Index data frame?

How can I emulate a stack frame in C++?

How can i disable variable with locals()?

Access the locals available in the previous stack frame

How can I update if value is different or empty

How can I create a data frame from a list of lists with different lengths in Python?

How can I force update React.memo child component?

React: how can I force state to update in a functional component?

How can I force user to update my android app with Github?

How can I force Vaadin v8 to update the screen?

How can I force update all the snapshot Gradle dependencies in intellij

How can I force electron to update rendering based on new css?

How can I analyze list values in a python dictionary, add extra dictionary values showing the different keys and the list value count in common?

How to map and update python dictionary with different key value pair?

How can I use python dictionary?

How can I generate a dictionary of lists in python?

How can I intersect values of a dictionary in python?

In a recursive function, how can I jump to a different function call on the stack?

How can i stack pandas dataframes with different column names vertically

Impossible to update textbox from another frame, how I can fix that?

How can I perform different operations in the same column of data frame?