-
-
Save omry/8b84f8f77ef431e6a50d8a892f865f5c to your computer and use it in GitHub Desktop.
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, *args, **kwargs): | |
print("run") | |
def __enter__(self, *args, **kwargs): | |
... | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
print("cleanup") | |
if __name__ == "__main__": | |
print("== context call:") | |
with dual_foo(): | |
pass | |
print("== standalone call:") | |
dual_foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
== context call:
run
cleanup
== standalone call:
run