Created
November 22, 2011 09:29
-
-
Save ykxpb/1385291 to your computer and use it in GitHub Desktop.
python with_statement
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
| # from PEP-343 | |
| mgr=statement | |
| exit=type(mgr).__exit__ #not calling it yet | |
| value = type(mgr).__enter__(mgr) | |
| exc = True | |
| try: | |
| try: | |
| var=value #only if "as var " is present | |
| block | |
| except: | |
| exc = False | |
| if not exit(mgr, *sys.exc_info()): #exit返回True将不抛出如何异常 | |
| raise | |
| finally: | |
| # The normal and non-local-goto cases are handled here | |
| if exc: | |
| exit(mgr, None, None, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment