[Python-checkins] cpython (2.7): Closes #14306: clarify expensiveness of try-except and update code snippet
georg.brandl
python-checkins at python.org
Sat Mar 17 16:58:54 CET 2012
http://hg.python.org/cpython/rev/7a93f6ee2ebc
changeset: 75783:7a93f6ee2ebc
branch: 2.7
parent: 75777:7c818b4cd98a
user: Georg Brandl <georg at python.org>
date: Sat Mar 17 16:58:05 2012 +0100
summary:
Closes #14306: clarify expensiveness of try-except and update code snippet
files:
Doc/faq/design.rst | 10 +++++-----
1 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -297,8 +297,9 @@
How fast are exceptions?
------------------------
-A try/except block is extremely efficient. Actually catching an exception is
-expensive. In versions of Python prior to 2.0 it was common to use this idiom::
+A try/except block is extremely efficient if no exceptions are raised. Actually
+catching an exception is expensive. In versions of Python prior to 2.0 it was
+common to use this idiom::
try:
value = mydict[key]
@@ -309,11 +310,10 @@
This only made sense when you expected the dict to have the key almost all the
time. If that wasn't the case, you coded it like this::
- if mydict.has_key(key):
+ if key in mydict:
value = mydict[key]
else:
- mydict[key] = getvalue(key)
- value = mydict[key]
+ value = mydict[key] = getvalue(key)
.. note::
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list