namespaces and eval

dave.g1234 at gmail.com dave.g1234 at gmail.com
Fri May 16 14:44:11 EDT 2008


Suppose I have a function in module X that calls eval e.g,

X.py
_______
Def foo(bar):
        Eval(bar)
_______

Now eval will be called using the default eval(bar,globals(),locals())
and globals will pull in anything in module X.

Now I have module Y that calls bar like so
Y.py
________
from x import *
def biz():
        print "Im a silly example!"
Foo("biz()")
_______

Python will complain that it cannot find biz because it's not the X
module.

My question - is there any way to a) get a listing of active namespaes
search ALL active namespaces for a particular variable b) get the
namespace of the caller withing passing it in (here, Y) c) do anything
else to get biz without passing foo("biz()",locals(),globals()) or
hacking biz into the __builtin__ namespace(don't know if it's even
possible, but it defeats the purpose of what I'm trying to do) ?  I
realize this is here for a reason, but I'm working on something that's
kind of a hack already

Thanks
dave


More gratuitous context. Long story I'm trying to write a hack for a
concise way of adding arbitrary methods to objects for JPA/JQuery like
chaning eg.

def foo(self):


class wrap:
    def __init__(self, obj, globalz=globals(),localz= locals()):
        self.obj = obj
        self.localz = localz;
        self.globalz = globalz;

    def __callback__(self, *args, **kw):
        newargs = [self.obj];
        newargs.extend(args);
        print locals
        ret = eval(self.name, self.globalz, self.localz)
(*newargs,**kw);
        return wrap(ret);

    def __getattr__(self,name):
        if hasattr(self.obj,name):
            return getattr(self.obj,name);
        self.name = name;
        return self.__callback__;









More information about the Python-list mailing list