[Python-checkins] peps: Note that context manager should be in separate classes if they implement

raymond.hettinger python-checkins at python.org
Tue Apr 24 08:34:34 CEST 2012


http://hg.python.org/peps/rev/4dc42ccf4a40
changeset:   4301:4dc42ccf4a40
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Apr 23 23:34:26 2012 -0700
summary:
  Note that context manager should be in separate classes if they implement non-obvious behaviors.

files:
  pep-0008.txt |  14 ++++++++++++++
  1 files changed, 14 insertions(+), 0 deletions(-)


diff --git a/pep-0008.txt b/pep-0008.txt
--- a/pep-0008.txt
+++ b/pep-0008.txt
@@ -837,6 +837,20 @@
           # Will also catch KeyError raised by handle_value()
           return key_not_found(key)
 
+- Context managers should be in separate classes whenever they do
+  something other than acquire and release resources.  For example:
+
+      Yes:     with auto_commit_or_rollback(conn):
+                   do_transaction(conn)
+
+      No:      with conn:
+                   do_transaction(conn)
+
+   The latter example doesn't provide any information to indicate that
+   the __enter__ and __exit__ methods are doing something other than
+   closing the connection after a transaction.  Being explicit is
+   important in this case.
+
 - Use string methods instead of the string module.
 
   String methods are always much faster and share the same API with

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list