Adding a method to an instance

Duncan Booth duncan at
Sat Mar 3 17:42:51 CET 2001

Bryan Mongeau <bryan at> wrote in <Sc0o6.361217$JT5.11613656>:

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

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

More information about the Python-list mailing list