Steven W. Orr wrote: > On Friday, Aug 24th 2007 at 09:12 -0700, quoth [EMAIL PROTECTED]: > > =>On Aug 24, 11:02 am, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > =>> In the program below, I want this instance to end up calling repmeth > =>> whenever inst.m1 is called. As it is now, I get this error: > =>> > =>> Hello from init > =>> inst = <__main__.CC instance at 0x402105ec> > =>> Traceback (most recent call last): > =>> File "./foo9.py", line 17, in ? > =>> inst.m1() > =>> TypeError: repmeth() takes exactly 1 argument (0 given) > =>> > =>> #! /usr/bin/python > =>> def repmeth( self ): > =>> print "repmeth" > =>> > =>> class CC: > =>> def __init__( self ): > =>> self.m1 = repmeth > =>> print 'Hello from init' > =>> > =>> def m1 ( self ): > =>> print "m1" > =>> > =>> inst = CC() > =>> inst.m1() > > =>Remove "self" from repmeth as it's not required in a function, only in > =>functions that are defined within a class. Of course, a function in a > =>class is also know as a method. > > Sorry. I need repmeth to have self passed to it automatically if it's > called. I didn't mean to obfuscate the problem by not making a reference > to self in repmeth.
At least you are consistent in that you obfuscate every question. Here's what you seem to want: import types def repmeth(self): print "repmeth" # inherit from object! class CC(object): def __init__(self): self.m1 = types.MethodType(repmeth, self) print 'Hello from init' def m1(self): print 'm1' inst = CC() inst.m1() Output: py> import types py> py> def repmeth(self): ... print "repmeth" ... py> # inherit from object! py> class CC(object): ... def __init__(self): ... self.m1 = types.MethodType(repmeth, self) ... print 'Hello from init' ... def m1(self): ... print 'm1' ... py> inst = CC() Hello from init py> inst.m1() repmeth James -- http://mail.python.org/mailman/listinfo/python-list