[Python-Dev] towards a faster Python

Chris Reedy creedy@mitretek.org
2003年6月09日 18:15:30 -0400


Neil Schemenauer wrote:
> The goal is that the compiler should be able to determine the scope of a
> name at compile time. Code like the above does not allow that and so
> non-local names must be searched for in both globals and builtins at run
> time. Unfortunately the warning is not bulletproof. It's possible to
> modify the module dict directly and bypass the warning. I'm not sure
> what to do about that. :-(

I think you might be looking for a test something like:
class special_dict(dict):
 """Special dictionary that doesn't allow changes to built-in names."""
 def __setitem__(self, key, value):
 if(key in self and hasattr(self[key], '__builtin_name__')
 and key == self[key].__builtin_name__):
 raise_or_warn_changing_builtin(self, key, value)
 super(special_dict, self).__setitem__(key,value)
where __builtin_name__ is a new special attribute that would appear only
on built-in objects that aren't supposed to have their names changed.
The class special_dict would be used in situations where these kinds of
changes aren't supposed to occur. (Of course, I'd expect this to be
implemented in C in the guts of the interpreter for the obvious
performance reasons.)
This approach allows code like, for example:
 globals().myownrange = range
 ...
 for i in myownrange(5):
 ...
 Chris
P.S. I've been a lurker on this list for some time. I now propose to
climb back under my rock.
This is informal and not an official Mitretek Systems position.
Dr. Christopher L. Reedy, Senior Principal Software Engineer
Mitretek Systems
Email: creedy@mitretek.org

AltStyle によって変換されたページ (->オリジナル) /