How do I dynamically change member variables using a function

Viewed 23

I'm trying to use a member function to change a variable, but would like the function to accept any variable, not create a new function for each one. How would I do this?

class Player:
    def __init__(self, name=None, armor=None, maxHP=None, currentHP=None):
        self.name = name
        self.armor = armor
        self.maxHP = maxHP
        self.currentHP = currentHP
    
    def setStat(self, stat, value):
        self.stat = value #Not valid Code, what should I change



John = Player(armor=5)

print(John.armor)

#This of course throws an error, but I'd like it to print 5 and then 10
John.setStat(armor, 10)
    
print(John.armor)
        
0 Answers
Related