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

Discussion in 'Python' started by Jacek Generowicz, Apr 8, 2004.

  1. 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()).
    Jacek Generowicz, Apr 8, 2004
    #1
    1. Advertising

  2. 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
    Daniel Dittmar, Apr 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    286
  2. Steven W. Orr
    Replies:
    6
    Views:
    265
    Steve Holden
    Apr 28, 2007
  3. Stef Mientki
    Replies:
    4
    Views:
    670
    Roy Smith
    Nov 15, 2010
  4. Chris Angelico
    Replies:
    0
    Views:
    143
    Chris Angelico
    Oct 11, 2012
  5. Chris Angelico
    Replies:
    0
    Views:
    147
    Chris Angelico
    Oct 14, 2012
Loading...

Share This Page