Skip to content

Instantly share code, notes, and snippets.

@raymondberg
Created June 13, 2018 04:26
Show Gist options
  • Save raymondberg/dff9f89bc7738001e63a639fff829914 to your computer and use it in GitHub Desktop.
Save raymondberg/dff9f89bc7738001e63a639fff829914 to your computer and use it in GitHub Desktop.
What happens to context managed resources in a return
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))
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