Python: How do I create effects that apply to variables in classes?
<p>I am working on a text-based RPG, and I am trying to create magic spells that affect stats such as base attack, turn order, etc. I am using various classes for the spells in the format:</p>

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

<p>and I am calling the spells from a dictionary </p>

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

<p>How would I use Flare on an enemy such as a goblin? I tried making a method</p>

<pre><code>def useBuffSpell(target, spell):
    target.spell.stat = target.spell.stat + spell.value
</code></pre>

<p>using parameters of <code>goblin</code> and <code>BuffSpell[1]</code>.</p>

<p>Additionally, what would be a good method of affecting next turn actions in a combat sequence via a spell?</p>

<p>Use <code>getattr</code> and <code>setattr</code>, and make the attribute reference in the spell a <code>str</code>:</p>

<pre><code>bardSpells = {
    1: BuffSpell(name= "Flare", level= 0, stat="baseAttack", value -1)
}

def useBuffSpell(target, spell):
    setattr(target, spell.stat,
            getattr(target, spell.stat, 0) + spell.value)
</code></pre>

<p><code>setattr</code> is a builtin that, given an object, the name of an attribute, and a value, sets that attribute of the object to the value.
<code>getattr</code> is a builtin that, given an object, the name of an attribute, and optionally a default (<code>0</code> in the above example), returns that attribute of the object, or the default if the object does not have an attribute by that name. If no default is given, nonexistent attribute access raises an <code>AttributeError</code>.</p>

<p>The above example, if you did <code>useBuffSpell(goblin, bardSpells[1])</code>, would reduce the <code>goblin</code>s <code>baseAttack</code> by 1, or set it to <code>-1</code> if it did not have a <code>baseAttack</code>.</p>

python class methods