Make context manager example likelier

This commit is contained in:
Antoine Pitrou 2012-04-24 14:08:00 +02:00
parent e7bef9d9f5
commit 36e5f61f80
1 changed files with 6 additions and 5 deletions

View File

@ -837,14 +837,15 @@ 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:
- Context managers should be invoked through separate functions or methods
whenever they do something other than acquire and release resources.
For example:
Yes: with auto_commit_or_rollback(conn):
do_transaction(conn)
Yes: with conn.begin_transaction():
do_stuff_in_transaction(conn)
No: with conn:
do_transaction(conn)
do_stuff_in_transaction(conn)
The latter example doesn't provide any information to indicate that
the __enter__ and __exit__ methods are doing something other than