with statement is very helpful in Python code not to forget close in file, for example. We can define it following a rule as below. Nice. We can build our with to make some process safe. This is a note for me.
class WithClass(object):
def __init__(self):
print('init')
def neko(self):
print('neko')
def __enter__(self):
print('enter')
return self
def __exit__(self, ex_type, ex_value, trace):
print('exit: {}, {}, {}'.format(ex_type, ex_value, trace))
def creator():
return WithClass()
with creator() as w:
w.neko()