Methods are class attributes - add method


In this example we are going to add a newly created method to the class. (monkey patching)

examples/oop/add_method.py
class Person():
    def __init__(self, name):
        self.name = name

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

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

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