Created
July 14, 2012 06:09
-
-
Save shomah4a/3109647 to your computer and use it in GitHub Desktop.
contextmanager のテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding:utf-8 -*- | |
import contextlib | |
def gencon(): | |
u''' | |
ふつうの contextmanager | |
''' | |
print 'enter' | |
yield 10 | |
print 'exit' | |
def gencon(): | |
u''' | |
__exit__ で 'generator が終わってねーぞあほか' って言われる | |
''' | |
print 'enter' | |
yield 10 | |
print 'exit' | |
yield 20 | |
if __name__ == '__main__': | |
with contextlib.GeneratorContextManager(gencon()) as x: | |
print x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yield書かずに関数抜ければStopIteration例外がでるのでその場合の例。