class B:
def __init__(self,filename,read):
self.fp = open(filename,read)
def __enter__(self):
return self.fp
def __exit__(self, exc_type, exc_val, exc_tb):
self.fp.close()
with B('b.txt','r') as fp:
print(fp.read())
hello world
Process finished with exit code 0