filtering keyword arguments

Amir amirnntp at gmail.com
Sat Jul 12 23:44:13 EDT 2008


How do you filter keyword arguments before passing them to a function?

For example:

def f(x=1): return x

def g(a, **kwargs): print a, f(**kwargs)

In [5]: g(1, x=3)
1 3

In [6]: g(1, x=3, y=4)
TypeError: f() got an unexpected keyword argument 'y'

Is there a way to do something like:

def g(a, **kwargs): print a, f(filter_rules(f, **kwargs))

so only {'x': 3} is passed to f?

I was hoping for a pythonic way of doing what in Mathematica is done
by FilterRules:

http://reference.wolfram.com/mathematica/ref/FilterRules.html




More information about the Python-list mailing list