Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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

1 Answer

0 votes
by (106k points)

To pass a method as a parameter in Python you can use the below-mentioned code:-

class Test:

def method1(self):

return 'hello world' 

def method2(self, methodToRun):

result = methodToRun() 

return result 

def method3(self):

return self.method2(self.method1)

test = Test()

print test.method3()

Browse Categories

...