[Python-ideas] Python hook just before NameError
Mark Lawrence
breamoreboy at yahoo.co.uk
Tue Dec 30 07:41:04 CET 2014
On 29/12/2014 11:43, Nathaniel Smith wrote:
> On 29 Dec 2014 07:46, "Mark Lawrence"
> <breamoreboy at yahoo.co.uk
> <mailto:breamoreboy at yahoo.co.uk>> wrote:
> >
> > On 28/12/2014 16:42, 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.
> >>
> >> Patch is here:
> >> http://bugs.python.org/issue23126
> >>
> >> 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
> >> interactively.
> >>
> >> Thoughts?
> >>
> >> ChrisA
> >
> >
> > +1 from me as I'm always forgetting the "obvious" imports such as sys
> and os when trying things interactively.
>
> I just set up a PYTHONSTARTUP script to pre-import the obvious things
> and that fixed this problem for me. You'd still have to make a startup
> script to set up the hook, and it's not like 'import os, sys' will
> appreciably affect startup time.
>
> -n
>
I should really get around to that, it's been at the back of my mind to
put it on the TODO list for about 12 years :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-ideas
mailing list