Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Ramchandra Apte
On Sunday, 7 October 2012 01:12:56 UTC+5:30, Terry Reedy wrote: > On 10/6/2012 7:36 AM, Dave Angel wrote: > > > > > 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.

Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Terry Reedy
On 10/6/2012 7:36 AM, Dave Angel wrote: 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), an

Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Dave Angel
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

Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Manuel Pégourié-Gonnard
Steven D'Aprano scripsit : > If you expect that most of the time the module will be found, the > try...except version will be faster. If you expect that most of the time > the module will not be found, the "if name in" version will be faster. > Ok. In the particular case of __import__, I guess

Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Manuel Pégourié-Gonnard
Günther Dietrich scripsit : > Somewhere I read a text regarding 'try:' versus 'if'. If you take the > probabitility into consideration, how many times the test will fail or > succeed, there are two possibilities: [...] Ok, thanks for the details! -- Manuel Pégourié-Gonnard - http://people.mat

Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Günther Dietrich
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. >t

Re: try/except KeyError vs "if name in ..."

2012-10-06 Thread Steven D'Aprano
On Sat, 06 Oct 2012 08:27:25 +0200, 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

try/except KeyError vs "if name in ..."

2012-10-05 Thread Manuel Pégourié-Gonnard
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]