Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Methods are class attributes - replace method

In this example we are going to replace the method in the class by a newly created function. (monkey patching)

class Person():
    def __init__(self, name):
        self.name = name

    def show(self):
        print(self.name)

y = Person('Jane')
print(y.name)    # Jane
y.show()         # Jane

def new_show(some_instance):
    print("Hello " + some_instance.name)

Person.show = new_show
y.show()         # Hello Jane