A second __builtins__- or globals()-like namespace.
Daniel Dittmar
daniel.dittmar at sap.com
Thu Apr 8 09:31:02 EDT 2004
Jacek Generowicz wrote:
> Python looks up names firstly in the nested function namespaces, then
> the global namespace, and finally in builtins, raising NameError if it
> fails to find a binding in any of those.
>
> Would it be possible (without hacking the interpreter) to add in
> another namespace somewhere into this search hierarchy?
You could try to replace the __builtins__ module with an object of your own
liking.
class IntermediateScope:
def __init__ (self, outerscope, **kwargs):
for name, value in kwargs.items ():
setattr (self, name, value)
self.outerscope = outerscope
def __getattr__ (self, name):
return getattr (self.outerscope, name)
sys.modules ['__builtins__'] = IntermediateScope (__builtins__, myvar =
myvalue)
I haven't tested whether this works at all and what the effect on error
messages could be.
Daniel
More information about the Python-list
mailing list