]> granicus.if.org Git - python/commitdiff
Closes #14306: clarify expensiveness of try-except and update code snippet
authorGeorg Brandl <georg@python.org>
Sat, 17 Mar 2012 15:58:05 +0000 (16:58 +0100)
committerGeorg Brandl <georg@python.org>
Sat, 17 Mar 2012 15:58:05 +0000 (16:58 +0100)
Doc/faq/design.rst

index 962b4ef4fc8622f3e60c20ae7af3a1d5353d6c3f..25c72db89b79052ee04d473f955cf570851c1e3e 100644 (file)
@@ -297,8 +297,9 @@ use the ``join()`` function from the string module, which allows you to write ::
 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 @@ expensive.  In versions of Python prior to 2.0 it was common to use this idiom::
 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::