質問
可能で宣言以上の変数を使用 with
決Python?
のようなもの:
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)
...又は清掃の二つを同時に問題なのでしょうか。
解決
これはV3.1 のためのPython 3で可能であり、< href = "http://docs.python.org/dev/whatsnew/2.7.html#other-language-changes" のrel = "noreferrer">はPython 2.7 を。新しい with
構文には、複数のコンテキストマネージャをサポートしています:
with A() as a, B() as b, C() as c:
doSomething(a,b,c)
contextlib.nested
とは異なり、これはa
とb
がその__exit__()
の場合でも呼ばれるC()
を持っていますか、それは__enter__()
方法だということを保証する例外を発生させます。
他のヒント
import contextlib
with contextlib.nested(open("out.txt","wt"), open("in.txt")) as (file_out, file_in):
...
更新:
を引用した文書に関する contextlib.nested
:
推奨されていませんから2.7版:を計算書に対応します 機能を直接なくエラーが癖).
見 Rafał Dowgirdの回答 ます。
あなたが行に変数を分割した場合、あなたは改行をラップするためにバックスラッシュを使用しなければならないことに注意してください。
with A() as a, \
B() as b, \
C() as c:
doSomething(a,b,c)
Pythonが代わりにタプルを作成するので括弧は、動作しません。
with (A(),
B(),
C()):
doSomething(a,b,c)
:タプルが__enter__
属性が不足しているので、は、エラーを(undescriptiveとクラスタイプを識別しない)を取得します
あなたは括弧内AttributeError: __enter__
as
を使用しようとすると、、Pythonは解析時にミスをキャッチします:
with (A() as a,
B() as b,
C() as c):
doSomething(a,b,c)
にSyntaxError:無効な構文
https://bugs.python.org/issue12782 にこの問題に関連しているようです。
私はあなたの代わりにこれをしたいと考えます:
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)
はPython 3.3以来、あなたはクラス ExitStack
する contextlib
のモジュールから。
それはあなたが扱うしようとしているどのように多くのファイルがわからない場合、それは特に有用であることが証明されることを意味し、コンテキストアウェアオブジェクトののダイナミックの番号を、管理することができます。
のドキュメントに記載されている標準的なユースケースは、ファイルの動的な数を管理している。
with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
# All opened files will automatically be closed at the end of
# the with statement, even if attempts to open files later
# in the list raise an exception
ここでは一般的な例です。
from contextlib import ExitStack
class X:
num = 1
def __init__(self):
self.num = X.num
X.num += 1
def __repr__(self):
cls = type(self)
return '{cls.__name__}{self.num}'.format(cls=cls, self=self)
def __enter__(self):
print('enter {!r}'.format(self))
return self.num
def __exit__(self, exc_type, exc_value, traceback):
print('exit {!r}'.format(self))
return True
xs = [X() for _ in range(3)]
with ExitStack() as stack:
print(stack._exit_callbacks)
nums = [stack.enter_context(x) for x in xs]
print(stack._exit_callbacks)
print(stack._exit_callbacks)
print(nums)
出力:
deque([])
enter X1
enter X2
enter X3
deque([<function ExitStack._push_cm_exit.<locals>._exit_wrapper at 0x7f5c95f86158>, <function ExitStack._push_cm_exit.<locals>._exit_wrapper at 0x7f5c95f861e0>, <function ExitStack._push_cm_exit.<locals>._exit_wrapper at 0x7f5c95f86268>])
exit X3
exit X2
exit X1
deque([])
[1, 2, 3]