diff options
author | Georg Brandl <georg@python.org> | 2012-03-17 15:58:05 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2012-03-17 15:58:05 (GMT) |
commit | f354f8e369e1e903d489e6dbac01f94d905dc022 (patch) | |
tree | 700521c8f7b4282097ba330944d0c7a18cdb8b3f | |
parent | e3d73544ad74eeb72e3e51794329b6bfa683ac55 (diff) | |
download | cpython-f354f8e369e1e903d489e6dbac01f94d905dc022.zip cpython-f354f8e369e1e903d489e6dbac01f94d905dc022.tar.gz cpython-f354f8e369e1e903d489e6dbac01f94d905dc022.tar.bz2 |
Closes #14306: clarify expensiveness of try-except and update code snippet
-rw-r--r-- | Doc/faq/design.rst | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index 962b4ef..25c72db 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -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:: |