是否可以在Python中使用with语句声明多个变量?
喜欢的东西:
from __future__ import with_statement
with open("out.txt","wt"), open("in.txt") as file_out, file_in:
for line in file_in:
file_out.write(line)
... 还是同时清理两个资源才是问题所在?
是否可以在Python中使用with语句声明多个变量?
喜欢的东西:
from __future__ import with_statement
with open("out.txt","wt"), open("in.txt") as file_out, file_in:
for line in file_in:
file_out.write(line)
... 还是同时清理两个资源才是问题所在?
当前回答
你也可以将创建上下文管理器(__init__方法)和进入上下文(__enter__方法)分开来增加可读性。所以不用写这段代码:
with Company(name, id) as company, Person(name, age, gender) as person, Vehicle(brand) as vehicle:
pass
你可以这样写:
company = Company(name, id)
person = Person(name, age, gender)
vehicle = Vehicle(brand)
with company, person, vehicle:
pass
注意,在with语句之外创建上下文管理器会给人一种印象,即创建的对象也可以在语句之外进一步使用。如果上下文管理器不是这样,那么错误的印象可能与可读性尝试相对应。
文件说:
大多数上下文管理器的编写方式意味着它们只能在with语句中有效地使用一次。这些单用途上下文管理器每次使用时都必须重新创建—尝试第二次使用它们将触发异常或无法正常工作。 这种常见的限制意味着,通常建议直接在with语句的头中创建上下文管理器。
其他回答
在Python 3.1+中,你可以指定多个上下文表达式,它们将被处理,就像多个with语句嵌套一样:
with A() as a, B() as b:
suite
等于
with A() as a:
with B() as b:
suite
这也意味着你可以在第二个表达式中使用第一个表达式的别名(在使用db连接/游标时很有用):
with get_conn() as conn, conn.cursor() as cursor:
cursor.execute(sql)
从Python 3.10开始,有一个括号上下文管理器的新特性,它允许以下语法:
with (
A() as a,
B() as b
):
do_something(a, b)
我认为你应该这样做:
from __future__ import with_statement
with open("out.txt","wt") as file_out:
with open("in.txt") as file_in:
for line in file_in:
file_out.write(line)
contextlib。Nested支持:
import contextlib
with contextlib.nested(open("out.txt","wt"), open("in.txt")) as (file_out, file_in):
...
更新: 引用文档,关于contextlib.nested:
2.7版后已移除:with-statement现在支持此功能 直接使用功能(没有容易出错的令人困惑的怪癖)。
更多信息请参见rafawarsaw Dowgird的回答。
你也可以将创建上下文管理器(__init__方法)和进入上下文(__enter__方法)分开来增加可读性。所以不用写这段代码:
with Company(name, id) as company, Person(name, age, gender) as person, Vehicle(brand) as vehicle:
pass
你可以这样写:
company = Company(name, id)
person = Person(name, age, gender)
vehicle = Vehicle(brand)
with company, person, vehicle:
pass
注意,在with语句之外创建上下文管理器会给人一种印象,即创建的对象也可以在语句之外进一步使用。如果上下文管理器不是这样,那么错误的印象可能与可读性尝试相对应。
文件说:
大多数上下文管理器的编写方式意味着它们只能在with语句中有效地使用一次。这些单用途上下文管理器每次使用时都必须重新创建—尝试第二次使用它们将触发异常或无法正常工作。 这种常见的限制意味着,通常建议直接在with语句的头中创建上下文管理器。