A second __builtins__- or globals()-like namespace.

Jacek Generowicz jacek.generowicz at cern.ch
Thu Apr 8 08:46:07 EDT 2004


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?






======================================================================

If you want more background ... 


I have an application which generates Python proxies for C++ objects,
automatically. For example, given

namespace foo {
  struct bar {
    void zot();
  };
}

the application automatically creates an object "foo" of type
Namespace, contaiting an attribute "bar" of type "Cclass", contaiting
an attribute "zot" of type "CMethod".

IOW, it's as if someone had written

class Namespace:
    pass

class

So far, I am storing the whole C++ scope tree (rooted at C++' global
namespace) in a variable, say "g", which makes the user refer to the
aforementioned "zot" as "g.foo.bar.zot". I would like to get rid of
the leading "g."  without polluting globals() or __builtins__ (and
providing a means of explicit disambiguation of any name
clashes). IOW, I would like an object (eg __cpp__), in which Python
tries to find the binding of a name after looking in globals() but
before looking in __builtins__ (or maybe after __builtins__, or even
before globals()).



More information about the Python-list mailing list