Created
January 4, 2020 06:30
-
-
Save ollien/fc1ef7a7ede021db5dfdfdaf2bbcaebb to your computer and use it in GitHub Desktop.
A decorator to close all given arguments
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
def close_args(*to_close): | |
def decorator(func: Callable): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
res = func(*args, **kwargs) | |
arg_names = inspect.getfullargspec(func)[0] | |
all_args = {**kwargs, **dict(zip(arg_names, args))} | |
for arg in to_close: | |
all_args[arg].close() | |
return res | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment