Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a method as a parameter in Python

Is it possible to pass a method as a parameter to a method?

self.method2(self.method1)  def method1(self):     return 'hello world'  def method2(self, methodToRun):     result = methodToRun.call()     return result 
like image 923
Dan Avatar asked Apr 01 '09 18:04

Dan


1 Answers

Yes it is, just use the name of the method, as you have written. Methods and functions are objects in Python, just like anything else, and you can pass them around the way you do variables. In fact, you can think about a method (or function) as a variable whose value is the actual callable code object.

Since you asked about methods, I'm using methods in the following examples, but note that everything below applies identically to functions (except without the self parameter).

To call a passed method or function, you just use the name it's bound to in the same way you would use the method's (or function's) regular name:

def method1(self):     return 'hello world'  def method2(self, methodToRun):     result = methodToRun()     return result  obj.method2(obj.method1) 

Note: I believe a __call__() method does exist, i.e. you could technically do methodToRun.__call__(), but you probably should never do so explicitly. __call__() is meant to be implemented, not to be invoked from your own code.

If you wanted method1 to be called with arguments, then things get a little bit more complicated. method2 has to be written with a bit of information about how to pass arguments to method1, and it needs to get values for those arguments from somewhere. For instance, if method1 is supposed to take one argument:

def method1(self, spam):     return 'hello ' + str(spam) 

then you could write method2 to call it with one argument that gets passed in:

def method2(self, methodToRun, spam_value):     return methodToRun(spam_value) 

or with an argument that it computes itself:

def method2(self, methodToRun):     spam_value = compute_some_value()     return methodToRun(spam_value) 

You can expand this to other combinations of values passed in and values computed, like

def method1(self, spam, ham):     return 'hello ' + str(spam) + ' and ' + str(ham)  def method2(self, methodToRun, ham_value):     spam_value = compute_some_value()     return methodToRun(spam_value, ham_value) 

or even with keyword arguments

def method2(self, methodToRun, ham_value):     spam_value = compute_some_value()     return methodToRun(spam_value, ham=ham_value) 

If you don't know, when writing method2, what arguments methodToRun is going to take, you can also use argument unpacking to call it in a generic way:

def method1(self, spam, ham):     return 'hello ' + str(spam) + ' and ' + str(ham)  def method2(self, methodToRun, positional_arguments, keyword_arguments):     return methodToRun(*positional_arguments, **keyword_arguments)  obj.method2(obj.method1, ['spam'], {'ham': 'ham'}) 

In this case positional_arguments needs to be a list or tuple or similar, and keyword_arguments is a dict or similar. In method2 you can modify positional_arguments and keyword_arguments (e.g. to add or remove certain arguments or change the values) before you call method1.

like image 68
David Z Avatar answered Oct 01 '22 10:10

David Z