Created
June 13, 2018 04:26
-
-
Save raymondberg/dff9f89bc7738001e63a639fff829914 to your computer and use it in GitHub Desktop.
What happens to context managed resources in a return
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
class Thing(): | |
def __init__(self): | |
self.name = 'thing' | |
print('setup') | |
def __enter__(self): | |
print('enter') | |
return self | |
def __exit__(self, *args): | |
self.name = None | |
def get_thing(): | |
with Thing() as thing: | |
print("connection: I got the {}".format(thing.name)) | |
return thing | |
thing = get_thing() | |
print("main: I got the {}".format(thing.name)) |
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
setup | |
enter | |
connection: I got the thing | |
exit | |
main: I got the None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment