Python number-like class that remembers arithmetic operations?
<p>I'm wondering if there exists a python module that would allow me to do something like this:</p>

<pre><code>x = MagicNumber()
x.value = 3
y = 2 * (x + 2) ** 2 - 8
print y   # 42
x.value = 2
print y   # 24
</code></pre>

<p>So <code>MagicNumber</code> would implement all the special operator methods, and they would all return instances of MagicNumber, while keeping track of what operations are performed. Is there such a class?</p>

<p><strong>EDIT: clarification</strong></p>

<p>I want to use this in a module that should remember a lot of parameters of some arbitrary calculation that the user wishes to perform. So the user will set the parameters and then use them to produce his result. Then if he decides he'd like to alter a parameter, the change is reflected in his result immediately. So a very simplified usage session with only one parameter instance would look like:</p>

<pre><code>p = MyParams()
p.distance = 13.4           # I use __getattr__ and __setattr__ such that
p.speed = 3.14              # __getattr__ returns MagicNumber instances
time = p.distance / p.speed
</code></pre>

<p><strong>EDIT 2: more clarification</strong></p>

<p>Okay, I'll do what I should have done from the start. I'll provide context.</p>

<p>You are an engineer and you're to produce a LaTeX document detailing the workings and properties of some prototype gadget. It is a task you'll do repeatedly for different prototypes. You write a small LaTeX python interface. For each prototype you create a python module that generates the requisite document. In it you type out the LaTeX code while calculating variables as they are needed, so that the calculations are in context. After a while you notice two problems:</p>

<ol>
<li>The number of variables and parameters makes locals a mess and the variable names are hard to remember. You should group them into categories to keep track of them all.</li>
<li>You sometimes need to redo the same calculation, which is spread over the last couple of chapters and a dozen lines, with one or more parameters changed. You should find some way to avoid code duplication.</li>
</ol>

<p>Out of this problem comes the original question.</p>
