How to modify the local namespace in python

喜欢而已 提交于 2019-11-27 09:08:24

You've a couple of options. First, note that g in your example isn't actually a local to the function (ie. not assigned within it), it's a global (ie hasn't been assigned to a local variable). This means that it will be looked up in the module the function is defined in. This is fortunate, as there's no way of altering locals externally (short of patching the bytecode), as they get assigned when the function runs, not before.

One option is simply to inject your function into the function's module's namespace. This will work, but will affect every function in that module that accesses the variable, rather than just the one function.

To affect just the one function, you need to instead point that func_globals somewhere else. Unfortunately, this is a read-only property, but you can do what you want by recreating the function with the same body, but a different global namespace:

import new
f = new.function(f.func_code, {'g': my_g_function}, f.func_name, f.func_defaults, f.func_closure)

f will now be indentical, except that it will look for globals in the provided dict. Note that this rebinds the whole global namespace - if there are variables there that f does look up, make sure you provide them too. This is also fairly hacky though, and may not work on versions of python other than cpython.

Since the function isn't invoked, it has no "local" stack frame, yet. The most simple solution is to use a global context:

handler = None
def f():
    handler()

def g(): pass

handler = g

Or you could set g on the function object:

f.g = g

But I'm not sure how you can get the function object from within the function itself. If it was a method, you would use self.

Why don't you just add an argument to f() and pass a reference to g()?

def g():
    pass

def f(func):
    func()

f(g)

I think you could solve the problem tackling it from a completely different point.
Functions are object, with their dictionaries; therefore, you can add g to f, and use it:

def g():
   print "g"

def f():
    f.g()

f.g = g

I assume you want to do this, because the function f is defined not by you, but by some other module. So you want to change how f() works. In particular, you want to change what is called when g is called.

So I'll suggest this:

import thirdpartypackage

def mynewg():
   pass

thirdpartypackage.g = mynewg

This will change the global g for the module thirdpartypackage. So when thirdpartypackage.f() now is called, it will call mynewg() instead of g().

If this doesn't solve it, maybe g() is in fact imported from withing f(), or somthing. Then the solution is this:

import thirdpartypackage

def mynewg():
   pass

deg mynewf():
   mynewg()

thirdpartypackage.f = mynewf

That is, you override f() completely with a modified version that does what you want it to.

A function that's not executing doesn't have any locals; the local context is created when you run the function, and destroyed when it exits, so there's no "local namespace" to modify from outside the function.

You can do something like this, though:

def f():
    g = [1]
    def func():
        print g[0]
    return func, g

f, val = f()
f()
val[0] = 2
f()

This uses an array to simulate a reference.

This seems to work

def add_to_locals(l):
    l['newlocal'] = 1

add_to_locals(locals())
assert newlocal
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!