Commit 2ba14289 authored by Georg Brandl's avatar Georg Brandl

merge with 3.2

parents 7add4eaa 12c3cd7c
...@@ -284,8 +284,9 @@ Similar methods exist for bytes and bytearray objects. ...@@ -284,8 +284,9 @@ Similar methods exist for bytes and bytearray objects.
How fast are exceptions? How fast are exceptions?
------------------------ ------------------------
A try/except block is extremely efficient. Actually catching an exception is A try/except block is extremely efficient if no exceptions are raised. Actually
expensive. In versions of Python prior to 2.0 it was common to use this idiom:: catching an exception is expensive. In versions of Python prior to 2.0 it was
common to use this idiom::
try: try:
value = mydict[key] value = mydict[key]
...@@ -296,11 +297,10 @@ expensive. In versions of Python prior to 2.0 it was common to use this idiom:: ...@@ -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 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:: time. If that wasn't the case, you coded it like this::
if mydict.has_key(key): if key in mydict:
value = mydict[key] value = mydict[key]
else: else:
mydict[key] = getvalue(key) value = mydict[key] = getvalue(key)
value = mydict[key]
For this specific case, you could also use ``value = dict.setdefault(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 getvalue(key))``, but only if the ``getvalue()`` call is cheap enough because it
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment