Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'subject:module': 0.04; 'francis': 0.07; 'imports': 0.07; '===': 0.09; 'conditional': 0.09; 'erik': 0.09; 'etc).': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'nameerror:': 0.09; 'namespace': 0.09; 'received:gator410.hostgator.com': 0.09; 'wrong,': 0.09; '~ethan~': 0.09; '>>>': 0.12; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'defined': 0.14; 'angelico': 0.16; 'dumps': 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'traceback': 0.16; '(most': 0.16; 'cc:addr:python-list': 0.17; 'header:In-Reply-To:1': 0.21; 'right.': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'last):': 0.23; 'fri,': 0.23; "what's": 0.23; 'pass': 0.27; 'import': 0.29; 'bound': 0.29; "he's": 0.29; 'bit': 0.30; 'cc:addr:python.org': 0.30; 'module': 0.30; 'annoying': 0.30; 'calling': 0.31; 'done,': 0.32; 'steven': 0.32; '...': 0.34; 'chris': 0.34; 'file': 0.34; 'header:User- Agent:1': 0.35; '"",': 0.35; '17,': 0.35; "d'aprano": 0.35; 'using': 0.35; 'quite': 0.36; 'could': 0.38; 'but': 0.38; 'execute': 0.38; 'subject:: ': 0.38; 'perhaps': 0.39; 'received:websitewelcome.com': 0.67; 'alternative': 0.71; 'extras': 0.84 Date: Thu, 16 Jun 2011 19:11:29 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Erik Max Francis Subject: Re: break in a module References: <4DF7E75E.9000907@mrabarnett.plus.com> <6NWdnfBF0rgB42fQnZ2dnUVZ5h2dnZ2d@giganews.com> <4dfaa441$0$30002$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:2698 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 3 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 63 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308275880 news.xs4all.nl 49042 [::ffff:82.94.164.166]:54333 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7790 Erik Max Francis wrote: > Chris Angelico wrote: >> On Fri, Jun 17, 2011 at 10:48 AM, Steven D'Aprano >> wrote: >>> Perhaps the most sensible alternative is conditional importing: >>> >>> # === module extras.py === >>> >>> def ham(): pass >>> def cheese(): pass >>> def salad(): pass >>> >>> >>> # === module other.py === >>> >>> def spam(): pass >>> >>> if not some_condition: from extras import * >>> >> >> This would, if I understand imports correctly, have ham() operate in >> one namespace and spam() in another. Depending on what's being done, >> that could be quite harmless, or it could be annoying (no sharing >> module-level constants, etc). > > No, he's using `from ... import *`. It dumps it all in the same > namespace Wrong, with a little bit right. The 'from ... *' functions are now bound in the calling namespace, but they still execute in their original namespace. Observe: 8<--these.py--------------------------------------------------------- import this this.yummy() 8<--this.py---------------------------------------------------------- breakfast = 'ham and eggs' def spam(): print(breakfast) if 'spam' not in breakfast: from that import * 8<--that.py---------------------------------------------------------- def yummy(): print(breakfast) 8<--results---------------------------------------------------------- --> import these Traceback (most recent call last): File "", line 1, in File "these.py", line 3, in this.yummy() File "that.py", line 2, in yummy print(breakfast) NameError: global name 'breakfast' is not defined 8<------------------------------------------------------------------- ~Ethan~