In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance.
import types
class Base:
def payload(self, *args):
print "Base:payload"
class Drvd(Base):
def iter(self, func):
derived_func = types.MethodType(func, self, Drvd) # bind
print "drvd_func:", derived_func
derived_func() # result: calls Base:payload
# expected:calls Drvd:payload; why???
def payload(self, *args):
print "Drvd:payload"
derived = Drvd()
base_func = Base.payload
print "base_func:", base_func
derived.iter(base_func) # pass unbound method object
The output shows:
base_func: <unbound method Base.payload>
drvd_func: <bound method Drvd.payload of <main.Drvd instance at 0x00B51648>>
Base:payload