Path: csiph.com!weretis.net!feeder4.news.weretis.net!ecngs!testfeeder.ecngs.de!81.171.118.64.MISMATCH!peer04.fr7!news.highwinds-media.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: module alias in import statement Date: Tue, 5 Apr 2016 17:47:29 +1000 Lines: 43 Message-ID: References: <570288d0$0$19758$426a74cc@news.free.fr> <47c51cb5-2d4b-42d3-8bf2-268720ebb4d2@googlegroups.com> <570368c2$0$1533$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de hG/FoivxHC+XlabD2WtOug5miWFH4UoBLWdZVLcM7Q6Q== 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; 'python,': 0.02; 'received:209.85.223': 0.03; 'cache': 0.05; 'importerror:': 0.05; 'defines': 0.07; 'granted,': 0.07; 'semantic': 0.07; 'cc:addr :python-list': 0.09; 'ast': 0.09; 'cached': 0.09; 'differently.': 0.09; 'imported': 0.09; 'newly': 0.09; 'semantics': 0.09; 'subject:module': 0.09; 'python': 0.10; '*ignore*': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object),': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'semantically': 0.16; 'sense,': 0.16; 'subject:import': 0.16; 'subsystem': 0.16; 'sys.modules': 0.16; 'wrote:': 0.16; 'typical': 0.18; '(in': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '"",': 0.22; "aren't": 0.22; 'tkinter': 0.22; 'trying': 0.22; 'defined': 0.23; '(you': 0.23; 'second': 0.24; 'import': 0.24; '(most': 0.24; 'header:In-Reply- To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'chris': 0.26; 'followed': 0.27; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'looks': 0.29; 'code': 0.30; 'entry': 0.31; 'point': 0.33; 'source': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'though.': 0.33; 'traceback': 0.33; 'case,': 0.34; 'tue,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'next': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'created': 0.36; 'received:209.85': 0.36; 'monday,': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'difference': 0.38; "won't": 0.38; 'received:209': 0.38; 'end': 0.39; 'goes': 0.39; 'system.': 0.39; 'still': 0.40; 'called': 0.40; 'back': 0.62; '(that': 0.63; 'different': 0.63; 'fundamental': 0.66; 'picked': 0.66; 'results': 0.66; 'miss': 0.77; '(your': 0.84; 'chrisa': 0.84; 'ttk': 0.84; 'utc+5:30,': 0.84; 'to:none': 0.91 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; bh=Lk6o5O/EhZzvmezKwuYEK9XZOjLio5vQyu/Z29rS6x0=; b=GoMuV27vOItxOSZ79IDh6UcCODTE4SRUj9S6Bf9xiP87n207z0s0ZO/6lA6lSkBFCE WNVitGiQPvIPL3em3NuUCYdBT7y0kF3XELziF+xhHpx//a9up6BnHB5IkJ3xnvHODmlB JQB0wCbaLhaT60UiUSIZ5nCQ0Ctr3jZtyG4W0Nm1ZCDWDh3WyNRJUtChkoXMYhm2MmI5 LcNrftWsJADReXmsLevJ4PeXSS3Vx4e+BjMZdhSnU56vmRtGuo+Ebv0ESnIGEzVt8Vrn 3ulMDJ3Gk5MlG/U/6gDqhPmk/HWupzJ+mC++N8Xy/XkvaWs74Wpvnr4MsyDBFg+O88Gx 7ylg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=Lk6o5O/EhZzvmezKwuYEK9XZOjLio5vQyu/Z29rS6x0=; b=RAlqDjWnGlO2j+IfM8jOMsJmM6V7nq11+qbQbiUZY7XBPGmkBE6maMW8Rx5GP9L7t0 +BiTt18vrNAt7QMC/xm6cMoV2V7PcDU97LZkNlXjTR0tHKP+4kg7njjJeXBt227tSPJA o8ejw+KtRjiufaIg9Yb6cT+g1vNdLySeLa3tLTxmOQJc5YHveMMBOgtjT2zOcxUj+lIr TQm2aGubgCOWO5wi0WcGQBtsFjp1rZJTbbzpcafkve66oenhOnDVJABfyd4YsTj9qq2Q jU/lY+xVRiVXxmiGGdaDKfIqUk6R3NdGdd96PxPRJbcuiDevL058PxCTs9P9RLpnEN74 wkXA== X-Gm-Message-State: AD7BkJIAByxJfCPTNKlwjmVZaED4jej7312tjI6nXit+gSQ9JltYijKQX2ELp1H+E3jLxIlEB0Rzu3KSotqJYg== X-Received: by 10.107.169.32 with SMTP id s32mr11034968ioe.19.1459842449840; Tue, 05 Apr 2016 00:47:29 -0700 (PDT) In-Reply-To: <570368c2$0$1533$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <570288d0$0$19758$426a74cc@news.free.fr> <47c51cb5-2d4b-42d3-8bf2-268720ebb4d2@googlegroups.com> <570368c2$0$1533$c3e8da3$5496439d@news.astraweb.com> X-Received-Bytes: 7375 X-Received-Body-CRC: 3609336490 Xref: csiph.com comp.lang.python:106500 On Tue, Apr 5, 2016 at 5:26 PM, Steven D'Aprano wrote: >>> sys.modules isn't really a cache in that sense, though. The "hard >>> problem" of cache invalidation comes from the fundamental assumption >>> that a cache hit should be semantically identical to a cache miss; >> >> Following looks like a cache miss to me (certainly did to the OP): >> >> On Monday, April 4, 2016 at 9:01:41 PM UTC+5:30, ast wrote: >>> hello >>> >>> >>> import tkinter as tk >>> >>> import tk.ttk as ttk >>> >>> Traceback (most recent call last): >>> File "", line 1, in >>> import tk.ttk as ttk >>> ImportError: No module named 'tk' > > > But that *miss* isn't cached -- if the OP then created a package called "tk" > with a submodule called "ttk", and then re-ran the `import tk.ttk as ttk` > line, the import subsystem would have picked up the newly created package > and imported it. > > I think that's the point Chris was trying to make. Not quite; that was a separate point (that negative results aren't cached). A cache miss is when the cache doesn't have something, and Python goes to the next source (in this case, searching the file system). The end result should be the same in either case (you get a module object), but in Python, a cache miss results in *actual code execution*. Granted, you often still won't *see* any difference (your typical module just quietly defines a bunch of stuff), but there's still a significant semantic difference; consider what happens when you type "import this as that" followed by "import this". The second one is a cache hit, and it's fundamentally different in function. Cache invalidation is all about knowing when you should *ignore* the cached entry and go back to the file system. Python will never do this, because the semantics are defined very differently. ChrisA