Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A second __builtins__- or globals()-like namespace.

Reply
Thread Tools

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

 
 
Jacek Generowicz
Guest
Posts: n/a
 
      04-08-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()).
 
Reply With Quote
 
 
 
 
Daniel Dittmar
Guest
Posts: n/a
 
      04-08-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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
command prompt opens for a split second =?Utf-8?B?TWNvc3Rl?= Wireless Networking 5 08-21-2005 09:30 PM
How do you add a second laptop to a network? ~AMDT~ Wireless Networking 1 08-18-2005 12:15 PM
Second instance of FF takes forever to load Al. C Firefox 2 03-27-2005 03:39 AM
Mozilla and second window Joebee Firefox 5 10-07-2003 05:03 PM



Advertisments