[Python-checkins] r54700 - peps/trunk/pep-0008.txt
guido.van.rossum
python-checkins at python.org
Fri Apr 6 17:09:23 CEST 2007
Author: guido.van.rossum
Date: Fri Apr 6 17:09:21 2007
New Revision: 54700
Modified:
peps/trunk/pep-0008.txt
Log:
Add words discouraging overly-broad except clauses.
Modified: peps/trunk/pep-0008.txt
==============================================================================
--- peps/trunk/pep-0008.txt (original)
+++ peps/trunk/pep-0008.txt Fri Apr 6 17:09:21 2007
@@ -689,6 +689,28 @@
the exception propagate upwards with 'raise'.
'try...finally' is a better way to handle this case.
+ - Additionally, for all try/except clauses, limit the 'try' clause
+ to the absolute minimum amount of code necessary. Again, this
+ avoids masking bugs.
+
+ Yes:
+
+ try:
+ value = collection[key]
+ except KeyError:
+ return key_not_found(key)
+ else:
+ return handle_value(value)
+
+ No:
+
+ try:
+ # Too broad!
+ return handle_value(collection[key])
+ except KeyError:
+ # Will also catch KeyError raised by handle_value()
+ return key_not_found(key)
+
- Use string methods instead of the string module.
String methods are always much faster and share the same API with
More information about the Python-checkins
mailing list