What Is The Best Way To Pass A Method (with Parameters) To Another Method In Python
Solution 1:
If you want to package the invocation up in one hit, you can use the functools module:
from functools import partial
defsome_function(param_one, param_two):
print"Param One: %s" % param_one
print"Param Two: %s" % param_two
defcalling_function(target):
target()
calling_function(partial(some_function, "foo", "bar"))
You can do tweakier things with functools.partial too, such as binding only some parameters, leaving you with a function with a new signature. It's overkill in a lot of cases to use it but it certainly has it's place.
Solution 2:
You could do it this way:
defmethod1(name):
defwrapper():
return'Hello ' + name
return wrapper
defmethod2(method, question):
output = method()
return output + ', ' + question
method2(method1(name = 'Sam'), 'How are you?')
You can of course pass some variables in the method() call too:
defmethod1(name):
defwrapper(greeting):
return greeting + name
return wrapper
defmethod2(method, question):
output = method(greeting = 'Hello ')
return output + ', ' + question
method2(method1(name = 'Sam'), 'How are you?')
Solution 3:
You can used functools.partial to do this, as jkp pointed out
However, functools is new in Python 2.5, so to handle this in the past I used the following code (this code is in the Python docs for functools.partial, in fact).
# functools is Python 2.5 only, so we create a different partialfn if we are# running a version without functools availabletry:
import functools
partialfn = functools.partial
except ImportError:
defpartialfn(func, *args, **keywords):
defnewfunc(*fargs, **fkeywords):
newkeywords = keywords.copy()
newkeywords.update(fkeywords)
return func(*(args + fargs), **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
Solution 4:
Another option, if you are working on a Python version pre 2.5 is to use a lambda as a closure:
defsome_func(bar):
print bar
defcall_other(other):
other()
call_other(lambda param="foo": some_func(param))
HTH
Solution 5:
You're thinking of currying, where you bind a function and arguments together to be called later. Usually currying is used so that you can add additional arguments at the time the function is actually called.
Rather than re-write the wheel, here's a link to an example: http://code.activestate.com/recipes/52549/.
If, however, the case you've mocked up in the question really is that simple, you can pass a list of args as positional parameters, or a list of kwargs as named parameters, to another function.
defmethod1(name):
return'Hello %s' % name
args = ['Joe']
method1(*args)
defmethod1a(name=None, salutation=None):
return'Hello %s %s' % (name, salutation)
kwargs = {'name':'Joe', 'salutation':'Mr'}
method1a(**kwargs)
Post a Comment for "What Is The Best Way To Pass A Method (with Parameters) To Another Method In Python"