Adding a method to an instance

Duncan Booth duncan at rcp.co.uk
Sat Mar 3 17:42:51 CET 2001


Bryan Mongeau <bryan at eevolved.com> wrote in <Sc0o6.361217$JT5.11613656
@news20.bellglobal.com>:

>Okay, based on these statements and referring to my previous example, I 
>should be able to do this:
>
>def newMethod(self):
>  print self.bar
>
>f.newMethod = newMethod
>
>Problem is:
>
>f.newMethod()
>TypeError: not enough arguments; expected 1, got 0 
>
>Understandably though, this works in this case:
>f.newMethod(f)

Try this:
    from new import instancemethod
    f = foo()
    f.newMethod = instancemethod(newMethod, f, foo)
    f.newMethod()
 



More information about the Python-list mailing list