Python Reference to Member Variable Which is Immutable
<p>I'm having a problem with not being able to reference a member variable indirectly. </p>

<p>I have a class with a function which does one thing. This function uses a member variable during its execution, and which member variable it uses is dependent on the input parameter cmd. Here's some code:</p>

<pre><code>class Foo(object):
    def __init__(self):
        self.a = 0
        self.b = 0

    def bar(self, cmd):
        if cmd == "do_this":
            index = self.a
        elif cmd == "do_that":
            index = self.b

        filename = "file-%d.txt" % index
        fp = open("filename", "w")

        # do some more things which depend on and *should* change
        # the value of self.a or self.b

        return
</code></pre>

<p>Now I understand why this code doesn't do what I want. Because we are using the immutable int type, our variable <strong>index</strong> refers to the value of the member variable <strong>self.a</strong> or <strong>self.b</strong>, not <strong>self.a</strong> or <strong>self.b</strong> <em>themselves</em>. By the end of the function, <strong>index</strong> refers to a different valued integer, but <strong>self.a</strong> and <strong>self.b</strong> are unchanged.</p>

<p>I'm obviously thinking too C++-ish. I want something equivalent to </p>

<pre><code>index = &amp;(self.a)
filename = "file-%d.txt" % *index
</code></pre>

<p>How do I accomplish this in a Pythonic way?</p>
