Can I use with statement with MySQLdb.Connection object?
问题 I wonder if I can use the context manager with with the Connection object, and write code like this: with MySQLdb.connect(...) as conn: do_something() Will the conn object be closed automatically after the block like with a file object ? Thanks. 回答1: MySQLdb does not support the context manager protocol. Roll your own, or use oursql instead. 来源: https://stackoverflow.com/questions/11751703/can-i-use-with-statement-with-mysqldb-connection-object