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)

examples/oop/replace_method.py
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