Function to apply superset of arguments to a function

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? Cheers, Andrey 1. http://gist.github.com/183375

functools.partial On Tue, Sep 8, 2009 at 9:07 PM, Andrey Fedorov<anfedorov@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?
Cheers, Andrey
1. http://gist.github.com/183375
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- Gerald Britton

This is the wrong list to ask for help with something like this. comp.lang.python/python-list is the proper place to try to get help. On Tue, Sep 8, 2009 at 18:07, Andrey Fedorov <anfedorov@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?
Cheers, Andrey
1. http://gist.github.com/183375
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
participants (3)
-
Andrey Fedorov
-
Brett Cannon
-
Gerald Britton