如果您使用的是python
2.5或更高版本,则可以使用以下
with语句:
from __future__ import with_statementimport contextlib@contextlib.contextmanagerdef handler(): try: yield except Exception, e: baz(e)
您的示例现在变为:
with handler(): foo(a, b)with handler(): bar(c, d)

如果您使用的是python
2.5或更高版本,则可以使用以下
with语句:
from __future__ import with_statementimport contextlib@contextlib.contextmanagerdef handler(): try: yield except Exception, e: baz(e)
您的示例现在变为:
with handler(): foo(a, b)with handler(): bar(c, d)