Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106492
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: module alias in import statement |
| Date | 2016-04-05 14:23 +1000 |
| Message-ID | <mailman.52.1459830198.32530.python-list@python.org> (permalink) |
| References | <570288d0$0$19758$426a74cc@news.free.fr> <ndujs3$7n6$1@ger.gmane.org> <mailman.38.1459802831.32530.python-list@python.org> <47c51cb5-2d4b-42d3-8bf2-268720ebb4d2@googlegroups.com> <CAPTjJmrskhvZBo5jbiDu4HGpp+YQ2u3sBj-ZFp3KcMv9e5LHyA@mail.gmail.com> |
On Tue, Apr 5, 2016 at 2:08 PM, Rustom Mody <rustompmody@gmail.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
module alias in import statement "ast" <nomail@com.invalid> - 2016-04-04 17:31 +0200
Re: module alias in import statement Ned Batchelder <ned@nedbatchelder.com> - 2016-04-04 08:59 -0700
Re: module alias in import statement Steven D'Aprano <steve@pearwood.info> - 2016-04-05 02:15 +1000
Re: module alias in import statement Terry Reedy <tjreedy@udel.edu> - 2016-04-04 16:46 -0400
Re: module alias in import statement Rustom Mody <rustompmody@gmail.com> - 2016-04-04 21:08 -0700
Re: module alias in import statement Chris Angelico <rosuav@gmail.com> - 2016-04-05 14:23 +1000
Re: module alias in import statement Rustom Mody <rustompmody@gmail.com> - 2016-04-04 21:27 -0700
Re: module alias in import statement Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-04-05 17:26 +1000
Re: module alias in import statement Chris Angelico <rosuav@gmail.com> - 2016-04-05 17:47 +1000
csiph-web