Skip to content

Instantly share code, notes, and snippets.

@reiki4040
Last active May 19, 2019 09:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reiki4040/5280066 to your computer and use it in GitHub Desktop.
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
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()
@MasahiroMizuno
Copy link

勉強させていただいています。
19行目の exc_typeはexception_typeではないでしょうか?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment