About Dynamically Assigned Object Methods

In Python you can bind a method to an object dynamically like this:

1
2
3
4
5
6
7
8
9
class TheClass(object):
    pass

def unbound_method(self, text):
    print(text)

obj = TheClass()
obj.method = unbound_method.__get__(obj, TheClass)
obj.method("Hello")

Tips and Tricks Programming Python 3