2010年5月17日月曜日

withステートメント

Understanding Python's "with" statement

基本構文
class controlled_execution:
    def __enter__(self):
        set things up
        return thing
    def __exit__(self, type, value, traceback):
        tear things down

with controlled_execution() as thing:
     some code

__exit__内で true value を返すと例外を投げるようです。
例えば、 return isinstance(value, TypeError) のように。

PEP 0343
The with statement (2.6リファレンス)

0 件のコメント:

コメントを投稿