-
-
Save omry/b691db9b7605f308f1e0a022ffacba44 to your computer and use it in GitHub Desktop.
dual_use_context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
init
enter
run
exit
init