【发布时间】:2013-12-14 21:57:38
【问题描述】:
使用 Python 可以说:
a,b,c=something_that_returns_a_3_tuple()
但是with 声明如下:
class thing(object):
def __enter__(self):
return (1,2,3)
def __exit__(self,a,b,c):
pass
with thing() as a,b,c:
print a
print b
print c
行不通
必须有:
class thing(object):
def __enter__(self):
return (1,2,3)
def __exit__(self,a,b,c):
pass
with thing() as (a,b,c):
print a
print b
print c
我看不出允许第一种形式的实际问题,我的意思是实现或逻辑,解析器不应该有逗号问题(它不会模棱两可),我看不出有什么合乎逻辑的理由。
http://docs.python.org/release/2.5/whatsnew/pep-343.html
和http://www.python.org/dev/peps/pep-0343/ 表明这只是合成糖
【问题讨论】:
标签: python with-statement contextmanager