global variables - Python: how to create a function pointer with a set argument? -
my problem:
given following:
def foo(a,b)
i trying call python 'map' function while passing in list 'a' use set value 'b.'
another relevant fact 'b' user input , thus, cannot use syntax:
def foo(a,b='default value')
i want 'map' call this:
map(foo_wrapper,list_for_a)
where 'foo_wrapper' function takes in 'a' uses user specified 'b.'
i don't know whether function pointers can specified way , suspect cannot.
my solution problem uses globals, if there's more elegant way , above impossible, mark answer well.
here solution in nutshell:
b = '' def foo(a,b): print b,a def foo_wrapper(a): foo(a,b) def main(): if sys.argv[1]: = ['john', 'jacob', 'jingle way'] global b b = sys.argv[1] map(foo_wrapper,a)
there may typo or 2 in above; simplifying problem need do.
thanks replies!
you can use functools.partial()
purpose:
from functools import partial def f(a, b): return + b x = range(10) print map(partial(f, b=3), x)
prints
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Comments
Post a Comment