Function to apply superset of arguments to a function

Carl Banks pavlovevidence at gmail.com
Wed Sep 9 18:12:08 EDT 2009


On Sep 9, 9:45 am, Andrey Fedorov <anfedo... at gmail.com> wrote:
> Hi all,
>
> I've written a function [1] called apply_some which takes a set of
> keywords arguments, filters only those a function is expecting, and
> calls the function with only those arguments. This is meant to
> suppress TypeErrors - a way to abstract the logic which checks what
> arguments a passed-in function accepts.
>
> For example:
>
> > def foo(x=1, y=2):
> >    return (x,y)
>
> > apply_some(foo, y=0, z="hi") // calls foo(y=0)
> > -> (1,0)
>
> I'd like to expand this to fill undefined arguments with None, but
> before I do, does anyone know of any packages/libraries which either
> do something similar or would make this code cleaner?

I don't know of any, but I would be surprised of the code you posted
could be made any cleaner.  Even if it's possible to improve your
implementation of apply_some, your usage is as clean and
straightforward as it gets.


Carl Banks



More information about the Python-list mailing list