python - How to pass predefined arguments when storing a function -
is possible store function predefined arguments called function?
for example:
def function(num): print num trigger=function #store function(1) trigger() #prints 1 trigger=function #store function(2) trigger() #prints 2
trigger calls whatever stored without passing arguments. can alter trigger require me rewrite function calls trigger, i'm wondering if there way store functions along arguments when storing function in variable.
you're looking functools.partial
:
>>> import functools >>> def foo(number): ... print number ... >>> bar = functools.partial(foo, 1) >>> bar() 1
python arguments
No comments:
Post a Comment