Skip to content

Instantly share code, notes, and snippets.

@omry
Created June 23, 2020 23:09
Show Gist options
  • Save omry/b691db9b7605f308f1e0a022ffacba44 to your computer and use it in GitHub Desktop.
Save omry/b691db9b7605f308f1e0a022ffacba44 to your computer and use it in GitHub Desktop.
dual_use_context
from contextlib import contextmanager
from typing import Any
@contextmanager
def foo() -> Any:
print("1 before")
yield
print("1 after")
class dual_foo:
def __init__(self):
print("init")
def __enter__(self, *args, **kwargs):
print("enter")
self.__call__(*args, **kwargs)
def __exit__(self, exc_type, exc_val, exc_tb):
print("exit")
def __call__(self, *args, **kwargs):
print("run")
if __name__ == "__main__":
with dual_foo():
pass
dual_foo()
@omry
Copy link
Author

omry commented Jun 23, 2020

init
enter
run
exit
init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment