Note that context manager should be in separate classes if they implement non-obvious behaviors.

This commit is contained in:
Raymond Hettinger 2012-04-23 23:34:26 -07:00
parent f0a17ba540
commit 660b6e478c
1 changed files with 14 additions and 0 deletions

View File

@ -837,6 +837,20 @@ Programming Recommendations
# 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