I’m trying to override globals so that any attribute name
that doesn’t exists returns itself(the name string).
The reason for this is to use it in eval to do some quick/hacky parsing of a c initilizer list, (and just because of course).
My Code:
class EvalGlobalsDict(dict):
def __getattr__(self, name):
if hasattr(self, name):
return super(EvalGlobalsDict, self).__getattr__(name)
else:
return name
eval_globals = EvalGlobalsDict(globals())
Whenever I try to eval a non existent name it gives me a NameError
eval("aaa",eval_globals)
And if I try to directly call
eval("globals().__getattr__("dir")",eval_globals)
eval("globals().__getattr__("dir")",eval_globals)
I only get back the attr name string even for valid attributtes.
What am I doing wrong?
Since you’re subclassing a dict, you’ll want to override
__getitem__(the indexing accessor).prints