Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(python': 0.05; 'modifying': 0.07; 'cc:addr:python-list': 0.09; 'imported': 0.09; 'instances.': 0.09; 'subject:module': 0.09; 'toss': 0.09; 'python': 0.10; 'importing': 0.15; '1:53': 0.16; 'bind': 0.16; 'code?': 0.16; 'confusion': 0.16; 'distinct': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "module's": 0.16; 'simpson': 0.16; 'wrote:': 0.16; 'string': 0.17; 'hack': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'prevent': 0.20; 'assumes': 0.22; 'mind.': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'command': 0.26; 'message-id:@mail.gmail.com': 0.27; 'back!': 0.29; "i'm": 0.30; 'code': 0.30; "i'd": 0.31; 'aside': 0.32; 'though,': 0.32; 'file': 0.34; 'that,': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'i.e.': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'requirement': 0.37; 'things': 0.38; "won't": 0.38; 'wrong': 0.38; 'anything': 0.38; 'does': 0.39; 'still': 0.40; 'some': 0.40; 'ever': 0.60; 'save': 0.60; 'real': 0.62; 'back': 0.62; 'skip:n 10': 0.62; 'course': 0.62; 'world': 0.64; 'accessed': 0.66; 'cameron': 0.66; '2.7.': 0.84; 'chrisa': 0.84; 'code):': 0.84; 'implications': 0.84; 'subject:official': 0.84; 'weaker': 0.84; 'to:none': 0.91; 'proposal.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=UyXwtfAzBefR0uvd4TO0dXUmScvISDwxRyZQNNIb3zs=; b=r/ZkWvccHVII2ZdHN8/4qMCPI0UtggN9TVOmqgF6BW1fGzF20dqiAcqXoLbnLWQTX4 orC+JcZ2PMLAJK8/LpGQFu8VF1Y/N4iZfXKBd1LdzQ4NTl4ZMY3hDXt2cly2Y5vV7hHk RMkBR9xOUzglOzIhziEBCcUZkYKfbGNktyW3l5ifcMc7C1xjUEUraJlYYgE1WIpHZ7LU ieYIGZdS7EgwLPeGuBItQWaHSeb/aEC9wYX21DI1uYXHKovJunGtnrF4LPDqyvE8S0Wd lpT05+bjeCUIIDE4tGjXPLmp2yhaUJfwd2DwdpPvdCsZYq6851EVDVNPahAsSnJR9lQY qMrw== MIME-Version: 1.0 X-Received: by 10.50.109.233 with SMTP id hv9mr16052269igb.92.1438488731472; Sat, 01 Aug 2015 21:12:11 -0700 (PDT) In-Reply-To: <20150802035327.GA53036@cskk.homeip.net> References: <20150802035327.GA53036@cskk.homeip.net> Date: Sun, 2 Aug 2015 14:12:11 +1000 Subject: Re: __main__ vs official module name: distinct module instances From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438488735 news.xs4all.nl 2842 [2001:888:2000:d::a6]:46000 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94886 On Sun, Aug 2, 2015 at 1:53 PM, Cameron Simpson wrote: > What are the implications of modifying the python invocation: > > python -m cs.app.maildb > > to effectively do this (Python pseudo code): > > M = importlib.import("cs.app.maildb") > M.__name__ = '__main__' > sys.modules['__main__'] = M > > i.e. import the module by name, but bind it to _both_ "cs.app.maildb" and > "__main__" in sys.modules. And of course hack .__name__ to support the > standard boilerplate. > > This would save some confusion when the module is invoked from the python > command line and also imported by the code; it is not intuitive that those > two things give you distinct module instances. > > Aside from the module's .__name__ being '__main__' even when accessed by the > code as cs.app.maildb, are there other implications to such a change that > would break real world code? That's the one implication that comes to mind. It breaks the invariant that "import X; X.__name__" should be the string form of X. But does anything ever actually depend on that, or do things depend on the weaker requirement that X.__name__ should be something which you can import to get back a reference to X? I'd support this proposal. I'm not seeing any glaring problems, and it does have the potential to prevent bizarre problems. It would still want to be a non-recommended course of action, though, as the change won't (presumably) be backported - so any code that assumes that importing its main file gives the same module as __main__ will silently do the wrong thing on Python 2.7. Toss it out onto -ideas and see what bounces back! ChrisA