filtering keyword arguments

A

Amir

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
 
P

Peter Otten

Amir said:
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 think you have to come up with something yourself. Here's a start:

import inspect

def filter_kw(f, **kw):
names = inspect.getargspec(f)[0]
return dict((n, kw[n]) for n in names if n in kw)

Peter
 
A

alex.gaynor

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

Sure, you could do:

def call_with_relevant_args(func, kwargs):
kwargs = dict([(k, v) for k, v in kwargs.iteritems() if k in
func.func_code.co_varnames])
return func(**kwargs)
 
B

bukzor

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

Here it is as a decorator:


def filter_arguments(func):
def func2(*args, **kwargs):
import inspect
arglist, vararg, kwarg, defaults = inspect.getargspec(func)
for k in kwargs.copy():
if k not in arglist: del kwargs[k]
return func(*args, **kwargs)
return func2


@filter_arguments
def func(a=1, b=2): return a+b


print func()
print func(c=3)
print func(a=3,b=4)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top