Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30885
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <d@davea.name> |
| 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; 'removes': 0.05; '(of': 0.07; 'caller': 0.07; 'try:': 0.07; 'python': 0.09; 'here?': 0.09; 'imported.': 0.09; 'imports': 0.09; 'loop.': 0.09; 'manuel': 0.09; 'mess': 0.09; 'modes': 0.09; 'terminates': 0.09; 'threads,': 0.09; 'cc:addr:python-list': 0.10; 'advance': 0.10; 'def': 0.10; 'thread': 0.11; 'assume': 0.11; 'library': 0.15; 'cases': 0.15; 'distinction': 0.16; 'flow.': 0.16; 'formulation': 0.16; 'path:': 0.16; 'return,': 0.16; 'sys.modules': 0.16; 'threaded': 0.16; 'threads': 0.16; 'try/except': 0.16; 'unload': 0.16; 'url:py3k': 0.16; 'wrote:': 0.17; 'network.': 0.17; 'test.': 0.17; '(in': 0.18; 'module': 0.19; 'trying': 0.21; 'import': 0.21; 'affected': 0.22; 'exceptions': 0.22; 'keyerror:': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'nearly': 0.23; 'seems': 0.23; 'raise': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'wondering': 0.26; '[1]': 0.27; 'am,': 0.27; 'checking': 0.27; 'question': 0.27; 'replace': 0.27; "doesn't": 0.28; 'subject:/': 0.28; 'concern': 0.29; 'convince': 0.29; 'equivalent.': 0.29; 'overhead': 0.29; 'testing.': 0.29; 'case,': 0.29; 'covered': 0.29; 'starts': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; "i'm": 0.29; 'maybe': 0.29; "we're": 0.30; 'function': 0.30; 'figure': 0.30; 'stuff': 0.30; 'code': 0.31; 'url:python': 0.32; 'generally': 0.32; 'not.': 0.32; 'structure': 0.32; 'hi,': 0.33; 'another': 0.33; 'done': 0.34; 'thanks': 0.34; 'similar': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'level.': 0.36; 'url:library': 0.36; 'test': 0.36; 'problems': 0.36; 'enough': 0.36; 'one,': 0.37; 'option': 0.37; 'quite': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'possible.': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'performance': 0.39; 'received:192': 0.39; 'application': 0.40; 'where': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'matter': 0.61; 'real': 0.61; 'policy': 0.62; 'between': 0.63; 'different': 0.63; 'subject:...': 0.63; 'success': 0.63; 'times': 0.63; 'more': 0.63; 'within': 0.64; 'here': 0.65; 'our': 0.65; 'subject': 0.66; 'header:Reply-To:1': 0.68; 'race': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'to:charset:iso-8859-1': 0.75; '(yet)': 0.84; 'conditions,': 0.84; 'maybe,': 0.84; 'subject:try': 0.84; 'suspecting': 0.84; 'url:imp': 0.84; 'treatments': 0.91; 'besides,': 0.93; 'responses': 0.93 |
| Date | Sat, 06 Oct 2012 07:36:11 -0400 |
| From | Dave Angel <d@davea.name> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 |
| MIME-Version | 1.0 |
| To | Manuel Pégourié-Gonnard <mpg@elzevir.fr> |
| Subject | Re: try/except KeyError vs "if name in ..." |
| References | <k4oj0d$qgl$1@thue.elzevir.fr> |
| In-Reply-To | <k4oj0d$qgl$1@thue.elzevir.fr> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 8bit |
| X-Provags-ID | V02:K0:bx7ew67qf2yn0lSM0QwJWfeBWwvHq5jr4JaZw/Gcyev 7ytalz/RpwVrgo23/7eJmtRY2F1aWhQwHNId7gu41jGbzHZK4e JLzKVsKqukSNNkbKTIb9susD3atQjCBLOnsQ3Wsk7UJaS4yAho IIfzZQWKEfvDMN7C5p1UbEVFRB96r9mp1EIdDrgXZJ3Mn9HNuz npx0zUazxEoxR8gK0VPtwFjzecjGhm0ciKfVXbUOsTfUqSW3q0 KXvI24KLTWZexHZHoVjDsio2e1UIMgN99dPdFlUnzjfz6gNNYJ 59/9zyG0SbSl3zJ1c5dHD6b2n5eHTeigv5uQdDIqO0CrX7liQ= = |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | d@davea.name |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1900.1349523406.27098.python-list@python.org> (permalink) |
| Lines | 76 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1349523406 news.xs4all.nl 6903 [2001:888:2000:d::a6]:46012 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:30885 |
Show key headers only | View raw
On 10/06/2012 02:27 AM, Manuel Pégourié-Gonnard wrote: > Hi, > > I was looking at the example found here [1] which begins with: > > [1] http://docs.python.org/py3k/library/imp.html#examples > > def __import__(name, globals=None, locals=None, fromlist=None): > # Fast path: see if the module has already been imported. > try: > return sys.modules[name] > except KeyError: > pass > > I was wondering if the formulation > > if name in sys.modules: > return sys.modules[name] > > would be equivalent. IOW, is using try/except here only a matter of > style or a necessity? > > I'm suspecting that maybe, in multithreaded environments, the second > option may be subject to a race condition, if another thread removes > name frome sys.modules between the if and the return, but as I'm not > very familiar (yet) with Python threads, I'm not sure it is a real > concern here. > > And maybe there are other reasons I'm completely missing for prefering > EAFP over LBYL here? > > Thanks in advance for your comments. > Guidelines for writing library code may very well be different than for writing your application. And if your application is trying to do something similar with *import*, chances are that it's calling a library function that already starts with the test against sys.modules. So if this is an application question, the answer is probably "don't do either one, just do the import, checking for the exceptions that it may throw." The distinction in performance between the success and failure modes of the try/catch isn't nearly as large as one of the other responses might lead you to believe. For example, a for loop generally terminates with a raise (of StopIteration exception), and that doesn't convince us to replace it with a while loop. Besides, in this case, the except code effectively includes the entire import, which would completely swamp the overhead of the raise. If we assume the question was more generally about EAFT vs. LBYL, and not just about the case of accessing the system data structure sys.modules, then the issues change somewhat. If we do a LBYL, we have to know that we've covered all interesting cases with our test. Multithreading is one case where we can get a race condition. There are times when we might be able to know either that there are not other threads, or that the other threads don't mess with the stuff we're testing. For example, there are enough problems with import and threads that we might just have a development policy that (in this program) we will do all our imports before starting any additional threads, and that we will never try to unload an import, single threaded or not. But for other conditions, we might be affected either by the system or by other processes within it. Or even affected by other asynchronous events over a network. If we do an EAFP, then we have to figure out what exceptions are possible. However, adding more exceptions with different treatments is quite easy, and they don't all have to be done at the same level. Some may be left for our caller to deal with. I think the major thing that people mind about try/catch is that it seems to break up the program flow. However, that paradigm grows on you as you get accustomed to it. -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
try/except KeyError vs "if name in ..." Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-06 08:27 +0200
Re: try/except KeyError vs "if name in ..." Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-06 08:33 +0000
Re: try/except KeyError vs "if name in ..." Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-06 12:08 +0200
Re: try/except KeyError vs "if name in ..." "Günther Dietrich" <gd.usenet@spamfence.net> - 2012-10-06 10:49 +0200
Re: try/except KeyError vs "if name in ..." Manuel Pégourié-Gonnard <mpg@elzevir.fr> - 2012-10-06 12:09 +0200
Re: try/except KeyError vs "if name in ..." Dave Angel <d@davea.name> - 2012-10-06 07:36 -0400
Re: try/except KeyError vs "if name in ..." Terry Reedy <tjreedy@udel.edu> - 2012-10-06 15:42 -0400
Re: try/except KeyError vs "if name in ..." Ramchandra Apte <maniandram01@gmail.com> - 2012-10-06 21:17 -0700
Re: try/except KeyError vs "if name in ..." Ramchandra Apte <maniandram01@gmail.com> - 2012-10-06 21:17 -0700
csiph-web