Last active
May 19, 2019 09:30
-
-
Save reiki4040/5280066 to your computer and use it in GitHub Desktop.
Example Python with statement 2013/8/24
fix LABEL comment at main(). LABEL was different to explain on blog.
http://d.hatena.ne.jp/reiki4040/20130331/1364723288
This file contains 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
class ExampleWithClass: | |
def __init__(self): | |
print('__init__') | |
def __del__(self): | |
print('__del__') | |
def __enter__(self): | |
print('__enter__') | |
#raise ValueError # [LABEL C] | |
return self | |
def __exit__(self, exception_type, exception_value, traceback): | |
print('__exit__') | |
self.close() | |
print(' exception_type:', exception_type) | |
print(' exception_value:', exception_value) | |
print(' traceback:', traceback) | |
#if exc_type: # [LABEL B] | |
# return True # [LABEL B] | |
def close(self): | |
print(' close()') | |
def show_msg(self, msg): | |
print(msg) | |
def main(): | |
print('--before with statement--') | |
try: | |
with ExampleWithClass() as ins: | |
print('do with block.') | |
#raise ValueError # [LABEL A] | |
ins.show_msg('print from ExampleWithClass.') | |
except: | |
print('catch exception') | |
print('--after with statement--') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
勉強させていただいています。
19行目の exc_typeはexception_typeではないでしょうか?