I am wondering if there is a python module that will allow me to do something like this:
x = MagicNumber() x.value = 3 y = 2 * (x + 2) ** 2 - 8 print y # 42 x.value = 2 print y # 24
So MagicNumber will implement all the special operator methods, and they all return MagicNumber instances, keeping track of which operations are performed. Is there such a class?
EDIT: clarification
I want to use this in a module that needs to remember many parameters of some arbitrary calculations that the user wants to perform. Thus, the user will set the parameters and then use them to get his result. Then, if he decides that he would like to change the parameter, this change will immediately affect its result. Thus, a very simplified use session with a single instance of the parameter will look like this:
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
EDIT 2: clarification
Well, I will do what I had to do from the very beginning. I provided the context.
You are an engineer, and you must prepare a LaTeX document that details the work and features of a prototype gadget. This is a task that you will do repeatedly for different prototypes. You are writing a small python LaTeX interface. For each prototype, you create a python module that generates the required document. In it, you enter LaTeX code when calculating variables as necessary, so that the calculations are in context. After a while, you will notice two problems:
- The number of variables and parameters makes the locals messy, and variable names are hard to remember. You must group them into categories to track them all.
- Sometimes you need to repeat the same calculation that spreads over the last couple of chapters and a dozen lines with a change in one or more parameters. You should find a way to avoid code duplication.
From this problem, an original question arises.
python
Lauritz V. Thaulow
source share