Skip to content

Instantly share code, notes, and snippets.

@omry
Created June 23, 2020 23:14
Show Gist options
  • Save omry/4804d4d0ba3d90808f42fb6c1bfa3172 to your computer and use it in GitHub Desktop.
Save omry/4804d4d0ba3d90808f42fb6c1bfa3172 to your computer and use it in GitHub Desktop.
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, *args, **kwargs):
self.__call__(*args, **kwargs)
def __enter__(self, *args, **kwargs):
print("enter")
def __exit__(self, exc_type, exc_val, exc_tb):
print("cleanup")
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

run
enter
cleanup
run

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