The same thing as you would do with self
if you were in a method. self
will be passed as the first parameter to your function and will be a reference to the current object. So if you want to define a function that will be used as a replacement for a method, just add self as the first parameter.
class SomeLibraryClass(object):
def __init__(self, x):
self.x = x
def foo(self, y):
print('Hello', self.x, y)
def my_foo(self, y):
print ('Good buy', self.x, y)
SomeLibraryClass.foo = my_foo
And how to use it:
>>> s = SomeLibraryClass(33)
>>> s.foo(5)
Good buy 33 5