Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
12c3cd7c
Commit
12c3cd7c
authored
Mar 17, 2012
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes #14306: clarify expensiveness of try-except and update code snippet
parent
f2123d2d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
5 deletions
+5
-5
Doc/faq/design.rst
Doc/faq/design.rst
+5
-5
No files found.
Doc/faq/design.rst
View file @
12c3cd7c
...
...
@@ -284,8 +284,9 @@ Similar methods exist for bytes and bytearray objects.
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]
...
...
@@ -296,11 +297,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)
For this specific case, you could also use ``value = dict.setdefault(key,
getvalue(key))``, but only if the ``getvalue()`` call is cheap enough because it
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment