[Python-ideas] Python hook just before NameError

Steve Dower Steve.Dower at microsoft.com
Sun Dec 28 18:03:47 CET 2014

+1 from me. The interactive uses would be great, and aren't really nice to implement without a hook like this.

Top-posted from my Windows Phone
From: Chris Angelico<mailto:rosuav at gmail.com>
Sent: ‎12/‎28/‎2014 10:42
To: python-ideas<mailto:python-ideas at python.org>
Subject: [Python-ideas] Python hook just before NameError

I mainly did up this patch to see how hard it would be, and now it's
turned out to be fairly simple, I'm curious as to whether it would
actually be useful to people.

At the point where a global/builtin name lookup is about to raise
NameError, first try calling a Python function, along the same lines
as __getattr__. If that function hasn't been defined, raise NameError
as normal; but if it has, let it either raise NameError or return some
object, which is then used as if the name had been bound to it.

Patch is here:

The idea is to allow convenient interactive use; auto-importing
modules is easy, and importing names from modules ("exp" --> math.exp)
can be done easily enough too, given a list of modules to try.

It's probably not a good idea to use this in application code, and I
definitely wouldn't encourage it in library code, but it has its uses


Python-ideas mailing list
Python-ideas at python.org
Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141228/585f968d/attachment-0001.html>

More information about the Python-ideas mailing list