Note that context manager should be in separate classes if they implement non-obvious behaviors.
This commit is contained in:
parent
f0a17ba540
commit
660b6e478c
14
pep-0008.txt
14
pep-0008.txt
|
@ -837,6 +837,20 @@ Programming Recommendations
|
||||||
# Will also catch KeyError raised by handle_value()
|
# Will also catch KeyError raised by handle_value()
|
||||||
return key_not_found(key)
|
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.
|
- Use string methods instead of the string module.
|
||||||
|
|
||||||
String methods are always much faster and share the same API with
|
String methods are always much faster and share the same API with
|
||||||
|
|
Loading…
Reference in New Issue