Passing functions with arguments to another function in Python?




Solution:


 with args*, kwargs** we can do these.......




===========================

Yes. By including the function call in your input argument/s, you can call two (or more) functions at once.
For example:
def anotherfunc(inputarg1, inputarg2):
    pass
def myfunc(func = anotherfunc):
    print func
When you call myfunc, you do this:
myfunc(anotherfunc(inputarg1, inputarg2))
This will print the return value of anotherfunc.
==========================


Yes.
def myfunc(anotherfunc, extraArgs):
    anotherfunc(*extraArgs)
To be more specific ... with various arguments ...
>>> def x(a,b):
...     print "param 1 %s param 2 %s"%(a,b)
... 
>>> def y(z,t):
...     z(*t)
... 
>>> y(x,("hello","manuel"))
param 1 hello param 2 manuel
>>> 

No comments:

Post a Comment