How to replace a method in an instance.

kyosohma at gmail.com kyosohma at gmail.com
Fri Aug 24 12:12:33 EDT 2007


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()
>
> TIA
>
> --
> Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
> happened but none stranger than this. Does your driver's license say Organ ..0
> Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
> individuals! What if this weren't a hypothetical question?
> steveo at syslang.net

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.

Mike




More information about the Python-list mailing list