[Python-ideas] Python hook just before NameError

Steven D'Aprano steve at pearwood.info
Mon Dec 29 01:37:42 CET 2014


On Mon, Dec 29, 2014 at 03:42:16AM +1100, Chris Angelico wrote:
> 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.

An interesting idea, but I don't actually think much of it for 
interactive use. Having modules magically import themselves without an 
import is a bad habit for beginners to learn, and less useful for 
experienced users who know to import things.

If I've understood it correctly, it's also process-wide global, rather 
than limited to a single module. That makes it much less useful, as it 
risks disguising bugs in library code.


-- 
Steven


More information about the Python-ideas mailing list