views:

687

answers:

6

How can I modify the local namespace of a function in python? I know that locals() returns the local namespace of the function when called inside it, but I want to do something like this (I have a reason why I want to do this where g is not accessible to f, but it's quicker to give a trivial, stupid example to illustrate the problem):

def g():
   pass

def f():
    g()

f.add_to_locals({'g':g})
+1  A: 

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.

Aaron Digulla
If you *really* wanted to use the function object inside the function, you could use inspect: import inspect import pprint def fn(): frame = inspect.currentframe() print frame.f_globals[frame.f_code.co_name].g fn.g = 'hello' fn()
Ivo
That's broken for class functions, nested functions/closures, and lambdas, and anything decorated--it'll return the decorator's wrapping function, not the actual function.
Glenn Maynard
+1  A: 

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.

Glenn Maynard
This site should really require explanations for -1 votes, so people can't anonymously vote down correct answers without a reason.
Glenn Maynard
A: 

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
Roberto Liffredo
Very elegant solution. +1
Josip
This only works for global functions; there's no way to access f reliably from within f.
Glenn Maynard
Could you expand this? Python functions are objects, and there are no differences if local or global - unless you mean _methods_ but that's a completely different story, and not what OP was asking for.
Roberto Liffredo
+1  A: 

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)
Steef
+1  A: 

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.

Lennart Regebro
+1  A: 

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.

Brian