Python: How do I refer to an attribute of a class by an attribute of another class or a variable?
<p>I am trying to create a magic system in my text-based RPG that refers to an attribute of a monster class using the attribute from the magic class. 
The monster class looks like </p>

<pre><code>class monster(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
</code></pre>

<p>with the list of monsters being store in the format </p>

<pre><code>bestiary = { 
99999: monster(name="Slime", currentHP= 3, maxHP= 10, initiativeMod= 1, AC= 0, baseAttack= 0, equippedWeapon= itemsList[13], speed = 10) ##Syntax items 
}
</code></pre>

<p>The spells are created in the form </p>

<pre><code>class BuffSpell(object):
def __init__(self, **kwargs):
    self.__dict__.update(kwargs)
</code></pre>

<p>with instances of each spell in the form </p>

<pre><code>bardSpells = {
    2: BuffSpell(name= "Flare", level= 0, stat= "baseAttack", value = -1, MP = 3, spellType = "buff"),
}
</code></pre>

<p>I am trying to refer to an attribute in monster that is given by an attribute in the spell like this </p>

<pre><code>def useMagic(target, spell):
if spell.spellType == "buff":
    x = spell.stat
    target.x += spell.value
</code></pre>

<p>which of course doesn't work. How can I get the spell.stat attribute and apply spell.value to the corresponding attribute in monster?</p>

<p>You could try something like the following:</p>

<pre><code>def use_magic(target, spell):
    if spell.spell_type == "buff":
        stat = spell.stat
        setattr(target, stat, getattr(target,stat) + spell.value)
</code></pre>

python attributes