Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'importing': 0.05; 'represents': 0.05; 'subject:Python': 0.06; 'method.': 0.07; 'source.': 0.07; 'absent': 0.09; 'happen.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:script': 0.09; 'suppress': 0.09; 'thrown': 0.09; 'python': 0.11; 'wrote': 0.14; 'attribute,': 0.16; 'attributes.': 0.16; 'be:': 0.16; 'globals': 0.16; 'globals(),': 0.16; 'importerror': 0.16; 'modules,': 0.16; 'raised,': 0.16; 'received:80.91.229.3': 0.16; 'received:97': 0.16; 'received:plane.gmane.org': 0.16; 'semantics': 0.16; 'subject:top': 0.16; 'userdict': 0.16; 'wrote:': 0.18; '(not': 0.18; 'module': 0.19; 'trying': 0.19; 'successful,': 0.19; 'work,': 0.20; 'import': 0.22; 'python?': 0.22; 'mon,': 0.24; 'equivalent': 0.26; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'feature': 0.29; 'am,': 0.29; "doesn't": 0.30; 'work.': 0.31; 'code': 0.31; 'raised': 0.31; 'class': 0.32; 'this.': 0.32; 'implemented': 0.33; 'trouble': 0.34; 'skip:_ 10': 0.34; 'subject:the': 0.34; "i'd": 0.34; 'could': 0.34; 'done.': 0.35; 'but': 0.35; 'add': 0.35; 'crazy': 0.36; 'module.': 0.36; 'next': 0.36; 'should': 0.36; 'changing': 0.37; 'being': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'challenge': 0.61; 'simple': 0.61; 'save': 0.62; 'within': 0.65; 'dict,': 0.84; 'dict.': 0.84; 'presumably': 0.84; 'slots.': 0.84; 'usage.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: __missing__ for the top-level Python script Date: Wed, 12 Nov 2014 10:16:15 -0500 (EST) Organization: news.gmane.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 97.73.240.77 X-Newsreader: PiaoHong.Usenet.Client.VIP:1.65 X-Archive: encrypt X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415805379 news.xs4all.nl 2957 [2001:888:2000:d::a6]:50575 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80609 Chris Angelico Wrote in message: > On Mon, Nov 10, 2014 at 10:31 AM, Chris Angelico 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