How to pass member function as argument in python?
dummy.func1
is unbound, and therefore simply takes an explicit self
argument:
def greet(f,name):
d = dummy()
f(d, name)
greet(dummy.func1,'Bala')
Since dummy
is the class name, dummy.func1
is unbound.
As phihag said, you can create an instance of dummy
to bind the method:
def greet(f,name):
d = dummy()
f(d, name)
greet(dummy.func1, 'Bala')
Alternatively, you can instantiate dummy
outside of greet
:
def greet(f,name):
f(name)
my_dummy = dummy()
greet(my_dummy.func, 'Bala')
You could also use functools.partial
:
from functools import partial
def greet(f,name):
f(name)
my_dummy = dummy()
greet(partial(dummy.func1, my_dummy), 'Bala')