Skip to content

Instantly share code, notes, and snippets.

@DavidVentura
Created August 24, 2020 17:56
Show Gist options
  • Save DavidVentura/7b8eb51947204dc032f611222de2f297 to your computer and use it in GitHub Desktop.
Save DavidVentura/7b8eb51947204dc032f611222de2f297 to your computer and use it in GitHub Desktop.
Stackable attributes in a function wrapper
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