Path: csiph.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 14:23:09 +1000 Lines: 33 Message-ID: References: <570288d0$0$19758$426a74cc@news.free.fr> <47c51cb5-2d4b-42d3-8bf2-268720ebb4d2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de pq+SXvovr8KSnJ7PonZgWAPm0l2hMErCO11OZi2O7NvQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'cache': 0.05; 'cc:addr:python-list': 0.09; 'imports': 0.09; 'subject:module': 0.09; 'python': 0.10; ':-)': 0.12; 'exception': 0.13; 'things.': 0.15; '2016': 0.16; 'confusion': 0.16; 'decimal;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'naming': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'semantically': 0.16; 'sense,': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'well-known': 0.16; 'wrote:': 0.16; 'module,': 0.18; 'resolved': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'so.': 0.22; 'seems': 0.23; "python's": 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'module': 0.25; 'message-id:@mail.gmail.com': 0.27; 'specifically': 0.28; 'looks': 0.29; 'decimal': 0.29; 'anyone': 0.32; 'another': 0.32; "can't": 0.32; 'says': 0.32; 'problem': 0.33; 'though.': 0.33; 'tue,': 0.34; 'skip:d 20': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'exist': 0.35; "isn't": 0.35; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'being': 0.37; 'things': 0.38; 'received:209': 0.38; 'anything': 0.38; 'means': 0.39; 'does': 0.39; 'where': 0.40; 'different': 0.63; 'fundamental': 0.66; 'guaranteed': 0.67; 'records': 0.70; 'chrisa': 0.84; 'mind:': 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=s6zYWEQKBMJ5T0rxj4HM0p5jxnTJ8I0AhL4axVI0R+8=; b=vfjoA8FVMFlUloGTnr54tpjnLygGYORWvMIXYD9jaWqx8T0Kv2eKVcj6WMSMnP2wFu lohTmMuAgEbbV3d5jWS5WiXf9s7UHY/kMDHTug1iMCUDGa4YALyOxKOMLc7MLeS3BI7+ ofVTtjGcP8MRiR0Hn9EIT7og06/7ixm7pXb5W8o/UtUhwQz04+d5P6Dfx3ristFRY5/D fo4xq2QN8xe/HbtHTbQE8BmfRbDxXhE2Tu4z+CYq0mSDL1rb2/NMHxoiJJTB39JeHBx7 eZcMFKhhC7jvCLD/EICidhsdjBn1+iqWLJmxhDtNxgu2oHuqirXj32ahK12fPYRuthSh 8O6g== 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=s6zYWEQKBMJ5T0rxj4HM0p5jxnTJ8I0AhL4axVI0R+8=; b=a5xrTN6+UobmhzGD6snT8C48t1rdi2adc3aHTVPKWRJrvWVq/VjjnGYabxiHkXKxZX 7BhUpx7MxJG+/T7Wq0J7FAP7W7L8qDhlj76NHaV4jBnBk75D51ZUucM7+RDs32r4mag/ bGKmEincpoiJbWR3EK/6v8w6OMG5auOvQgAbu+e52PR2jNd43RpUNZVoEXuOdjZvS5HI goZJEIIM6XALCn4SGx5w8rP0ZAsx48rf37TxTwdF4S2E9920uTPRnwYRV6+HIf+mfDr5 QwKbTUrNiG6mSDFuPFD6gcN3zDc0xIqPvzJ53y/vmXw434JOklmhf27G1S2ACbdvhsSN Vmzg== X-Gm-Message-State: AD7BkJI0MEK088kBqVKBmGm9XwKjdk2fL3XyCtPp2FzwKnmOWyPGd/g5tf+iq5yvN5x20sypQ3XDWX8dOIDEVA== X-Received: by 10.107.169.32 with SMTP id s32mr10452230ioe.19.1459830189820; Mon, 04 Apr 2016 21:23:09 -0700 (PDT) In-Reply-To: <47c51cb5-2d4b-42d3-8bf2-268720ebb4d2@googlegroups.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> Xref: csiph.com comp.lang.python:106492 On Tue, Apr 5, 2016 at 2:08 PM, Rustom Mody wrote: >> 'import tk.ttk' looks for 'tk' in sys.modules, does not find it, looks >> for a module named 'tk' on disk, does not find it, and says so. > > A well-known quote comes to mind: > > | There are only two hard things in Computer Science: cache invalidation and > | naming things. > > eg. http://martinfowler.com/bliki/TwoHardThings.html > > particularly since this seems to be in both categories :-) 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; Python's import system has fundamentally different semantics, specifically that modules can exist independently of anything on the disk, and modules are guaranteed to be singletons - which means that "import decimal; decimal.getcontext().prec = 50" will affect anyone else who uses the decimal module, because there can't be a duplicate. (The one exception to this, where __main__ gets reimported under another name, causes significant confusion despite being extremely uncommon.) If you like, you can look at sys.modules as a mandatory cache that never records negatives and always records positives. Once an import has succeeded, it will always be resolved from the cache, until program termination; failed imports will always be retried. Python dodges the cache invalidation problem by never invalidating anything :) ChrisA