BadPython.com

Monkey Patching an instance using MethodType

        
from types import MethodType

class Character():
    def __init__(self, attack_power, hp):
        self.hp = hp
        self.attack_power = attack_power

def player_attack(self, target):
    print("I'm a player character!")
    target.hp -= self.attack_power
    
def monster_attack(self, target):
    print("I'm a monster!! RAWR!!")
    target.hp -= self.attack_power


#create a player character
player = Character(5, 100)
player.attack = MethodType(player_attack, player)

#create a monster character
monster = Character(10, 100)
monster.attack = MethodType(monster_attack, player)

#have the monster attack the player
monster.attack(player)

print(f"After being attacked, the player has {player.hp} health left")