contextmanager

Create a “with” block on several context managers? [duplicate]

丶灬走出姿态 提交于 2019-11-26 03:56:11
This question already has an answer here: Multiple variables in a 'with' statement? 5 answers Suppose you have three objects you acquire via context manager, for instance A lock, a db connection and an ip socket. You can acquire them by: with lock: with db_con: with socket: #do stuff But is there a way to do it in one block? something like with lock,db_con,socket: #do stuff Furthermore, is it possible, given an array of unknown length of objects that have context managers, is it possible to somehow do: a=[lock1, lock2, lock3, db_con1, socket, db_con2] with a as res: #now all objects in array

Create a “with” block on several context managers? [duplicate]

最后都变了- 提交于 2019-11-26 01:14:27
问题 This question already has an answer here: Multiple variables in a 'with' statement? 6 answers Suppose you have three objects you acquire via context manager, for instance A lock, a db connection and an ip socket. You can acquire them by: with lock: with db_con: with socket: #do stuff But is there a way to do it in one block? something like with lock,db_con,socket: #do stuff Furthermore, is it possible, given an array of unknown length of objects that have context managers, is it possible to