diff --git a/contextmanagers.py b/contextmanagers.py index 138c6d9..021de87 100644 --- a/contextmanagers.py +++ b/contextmanagers.py @@ -12,29 +12,26 @@ @contextlib.contextmanager def unlock(resource): - resource = "unlocked" + resource.locked = False try: - yield resource + yield finally: - resource = "locked" + resource.locked = True # a resource that is locked -resource = "locked" +class Resource: + def __init__(self): + self.locked = True + +resource = Resource() # test that it is indeed locked -print(resource) +print(resource.locked) # call your 'unlock' context manager with your resource -with unlock(resource) as unlocked: - print(unlocked) # check that it is unlocked +with unlock(resource): + print(resource.locked) # check that it is unlocked # ensure it was re-locked when it left the 'unlock' context -print(resource) - - - - - - - +print(resource.locked) diff --git a/metatable.py b/metatable.py index 040079c..caec968 100644 --- a/metatable.py +++ b/metatable.py @@ -1,5 +1,5 @@ """ -metatable with where the function recieves the dictionary and key. +metatable with where the function receives the dictionary and key. """ from collections import defaultdict