Create a gist now

Instantly share code, notes, and snippets.

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment