Dan
Dan

Reputation: 3756

What is the best way to pass a method (with parameters) to another method in python

What's the best way to pass a method and a method parameter to another method?

Is there a better way to do the following?

def method1(name)
    return 'Hello ' + name

def method2(methodToCall, methodToCallParams, question):
    greetings = methodToCall(methodToCallParams)
    return greetings + ', ' + question

method2(method1, 'Sam', 'How are you?')

Upvotes: 2

Views: 798

Answers (5)

Jay
Jay

Reputation: 42642

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 available
try:
    import functools
    partialfn = functools.partial
except ImportError:
    def partialfn(func, *args, **keywords):
        def newfunc(*fargs, **fkeywords):
            newkeywords = keywords.copy()
            newkeywords.update(fkeywords)
            return func(*(args + fargs), **newkeywords)
        newfunc.func = func
        newfunc.args = args
        newfunc.keywords = keywords
        return newfunc

Upvotes: 2

jkp
jkp

Reputation: 81278

Another option, if you are working on a Python version pre 2.5 is to use a lambda as a closure:

def some_func(bar):
    print bar

def call_other(other):
    other()

call_other(lambda param="foo": some_func(param))

HTH

Upvotes: 1

Caotic
Caotic

Reputation: 964

You could do it this way:

def method1(name):
    def wrapper():
        return 'Hello ' + name
    return wrapper

def method2(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:

def method1(name):
    def wrapper(greeting):
        return greeting + name
    return wrapper

def method2(method, question):
    output = method(greeting = 'Hello ')
    return output + ', ' + question

method2(method1(name = 'Sam'), 'How are you?')

Upvotes: 3

jkp
jkp

Reputation: 81278

If you want to package the invocation up in one hit, you can use the functools module:

from functools import partial

def some_function(param_one, param_two):
    print "Param One: %s" % param_one
    print "Param Two: %s" % param_two

def calling_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.

Upvotes: 11

Jarret Hardie
Jarret Hardie

Reputation: 97912

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.

def method1(name):
    return 'Hello %s' % name

args = ['Joe']
method1(*args)

def method1a(name=None, salutation=None):
    return 'Hello %s %s' % (name, salutation)

kwargs = {'name':'Joe', 'salutation':'Mr'}
method1a(**kwargs)

Upvotes: 0

Related Questions