Created
August 24, 2020 17:56
-
-
Save DavidVentura/7b8eb51947204dc032f611222de2f297 to your computer and use it in GitHub Desktop.
Stackable attributes in a function wrapper
This file contains hidden or 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 functools import wraps | |
def autocomplete(key, complete_fn): | |
def deco(f): | |
@wraps(f) | |
def wrapper(*args, **kwargs): | |
return f(*args, **kwargs) | |
if hasattr(wrapper, 'complete_fn'): | |
wrapper.complete_fn[key] = complete_fn | |
else: | |
wrapper.complete_fn = {key: complete_fn} | |
return wrapper | |
return deco | |
@autocomplete('val1', list) | |
@autocomplete('val2', sum) | |
def some_fn(): | |
"""some doc""" | |
print('hi') | |
some_fn() | |
print(some_fn.complete_fn) | |
print(some_fn.__doc__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment