Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80609
| From | Dave Angel <davea@davea.name> |
|---|---|
| Subject | Re: __missing__ for the top-level Python script |
| Date | 2014-11-12 10:16 -0500 |
| Organization | news.gmane.org |
| References | <CAPTjJmpVVfPzwrbeornk9Txyfiz+H5MDfHG2nY_9va7RCUEmEw@mail.gmail.com> <CAPTjJmrhz7crC2_x+ZN9sLgPvd8B1Yn8skzMSTBXJ6gSj55TqQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15725.1415805379.18130.python-list@python.org> (permalink) |
Chris Angelico <rosuav@gmail.com> Wrote in message: > On Mon, Nov 10, 2014 at 10:31 AM, Chris Angelico <rosuav@gmail.com> wrote: >> So the semantics should be: If NameError would be raised (not >> including UnboundLocalError, which still represents an error), attempt >> to import the absent name. If successful, continue as if it had >> already been done. If ImportError is raised, suppress it and let the >> original NameError happen. > > No bites? I'd have thought there'd be a few crazy ideas thrown out in > answer to this. > > What if it's worded as a feature for interactive Python? Save you the > trouble of explicitly importing modules, by auto-importing them in > response to usage. In theory, it's as simple as adding __missing__ to > globals(), but I don't know of a way to do that for the main module. > > ChrisA > I gave it a short whirl, just trying to make __missing__ work. The type of globals () is a dict. I was able to add a __missing__:myfunct to the instance but in order to work, the __missing__ must be added as a class attribute, a method. And dict, being implemented in C, doesn't let you add class attributes. Presumably the C code does the equivalent of slots. So the next thing to try would be to try to force the module dictionary to be a userdict without slots. Then we could add the necessary method. But that would be changing the python source. I wasn't sure that was within the challenge constraints. -- DaveA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: __missing__ for the top-level Python script Dave Angel <davea@davea.name> - 2014-11-12 10:16 -0500
csiph-web