How to replace a method in an instance.

J. Cliff Dyer jcd at sdf.lonestar.org
Fri Aug 24 15:20:56 EDT 2007


Steven W. Orr wrote:
> On Friday, Aug 24th 2007 at 12:26 -0400, quoth Steven W. Orr:
>
> =>On Friday, Aug 24th 2007 at 09:12 -0700, quoth kyosohma at gmail.com:
> =>
> =>=>On Aug 24, 11:02 am, "Steven W. Orr" <ste... at syslang.net> 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. 
> =>
> =>Am I being clear?
>
> On Friday, Aug 24th 2007 at 18:44 +0200, quoth Wildemar Wildenburger:
>
> =>Steven W. Orr wrote:
> =>> 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.
> =>>
> =>> Am I being clear?
> =>>
> =>>
> =>Sort of. Maybe you fare better if you state what you want to achieve,
> =>instead of how you think you should do it. Chances are you don't need to
> =>replace a method in __init__, but there's another, less tricky way.
>   
> Ok. I have a collection of classes that are produced by a factory. They   
> all inherit from a baseclass. One (maybe more) of the classes inherits a  
> method that he shouldn't. All I want is to be able to change that
> particular class so that he will have the special method that he needs    
> instead of the default one that he inherits. I was thinking that instead 
> of making a special class for CC to inherit from which would give him his 
> special replacement method(s), I could simply assign them in a manner 
> which would more easily lend itself to later being table driven.
>
> If I had a choice, I'd rather not do it in init. Instead, I'd rather be 
> able to say something like 
> CC.m1 = repmeth
> but since in the real world, CC inherits from his baseclass, the above 
> assignment causes the baseclass to be altered. :-(
>
> Please tell me if I need to provide more.
>
>   
Is there a reason you don't just create a subclass for the one that 
needs to call repmeth?

class CC(object):
    def m1(self):
       print "m1"
    def m2(self):
       print "m2"

class SpecialCC(CC):
    def m1(self):
       print "something else!"

if __name__ == '__main__':
    a = CC()
    b = SpecialCC()
    for instance in a, b:
        instance.m1()
        instance.m2()

--Output--
m1
m2
something else!
m2


Is that what you're looking for, mas o menos?

Cheers,
Cliff 



More information about the Python-list mailing list