Skip to content

Instantly share code, notes, and snippets.

@foowaa
Created December 17, 2018 07:00
Show Gist options
  • Save foowaa/8f7d7c7f301d060926f8894cf8e558f6 to your computer and use it in GitHub Desktop.
Save foowaa/8f7d7c7f301d060926f8894cf8e558f6 to your computer and use it in GitHub Desktop.
import contextlib
@contextlib.contextmanager
def open_func(file_name):
# __enter__方法
print('open file:', file_name, 'in __enter__')
file_handler = open(file_name, 'r')
yield file_handler
# __exit__方法
print('close file:', file_name, 'in __exit__')
file_handler.close()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment