Created
May 21, 2014 09:25
-
-
Save wolf0403/5abdafe6afffa78794d7 to your computer and use it in GitHub Desktop.
Arg Sink decorator
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
# When calling functions, remove unused arguments | |
# Useful for framework callbacks | |
def args_sink(f): | |
import inspect | |
dst_args = inspect.getargspec(f) | |
args = set(dst_args.args) | |
@wraps(f) | |
def wrapper(*a, **kw): | |
nkw = deepcopy(kw) | |
if not dst_args.varargs: | |
a = a[:len(dst_args.args)] | |
if not dst_args.keywords: | |
for k in kw: | |
if k not in args: | |
del nkw[k] | |
return f(*a, **nkw) | |
return wrapper | |
# Example: | |
t = lambda: 0 | |
args_sink(t)(a=1, b=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment