python - API for context managers -
i have object (say fileproxy) looks file work. initialised foo = fileproxy("/some/file.txt") , can use so
foo.open() foo.write("something") foo.close() i want make work context manager. question whether should make work context manager more creating object calls .open method , .close upon exit.
with fileproxy("/some/file.txt") f: f.write("something") or should like
with fileproxy("/some/file.txt").open() f: f.write("something") which more symmetrical non context manager api.
i prefer former myself looks different way regular files in python work , i'm wondering if there's anti pattern i'm implementing here.
Comments
Post a Comment