Update a list every time a function within a class is executexecuted with the function arguments and values
<p>In the last 6 months, I have created many classes and function for different calculations. Now I would like to join them into a class so it could be used by other users. Like many of the methods can be performed in sequence and the sequence may require to be run multiple times for different sets of data. I would like to create a function within the main class that saves the procedure used by the user so the user could  run it again without going through the whole procedure again.</p>

<p>Important: I would not like to change the classes that have already been implemented, but if this is the only way, no problem.</p>

<p>The idea would be more or less like the code represented below:</p>

<pre><code>class process(object):
    def __init__(self):
        self.procedure = []

    def MethodA(valueA):
        pass

    def MethodB(valueB):
        pass

    def UpdateProcedure(self, method, arguments, values):
        # Execute this every time that MethodA or MethodB is executed.
        new = {'Method':method, 'Arguments':arguments, 'Values':values}
        self.procedure.append(new)
</code></pre>

<p>For example:</p>

<pre><code>a = process.MethodA(2)
b = process.MethodB(3)

print(process.procedure)
&gt;&gt;&gt; [{'Method':'MethodA', 'Arguments':['valueA'], 'Values':[2]}, {'Method':'MethodB', 'Arguments':['valueB'], 'Values':[3]}]
</code></pre>

<p>I have tried to use the functions <code>inspect.currentframe</code> and <code>__getattribute__</code> together, but I did not get good results.</p>

<p>Any ideas to help me with this issue?</p>

<p>Thanks</p>

<p>Here's how to use a decorator, per Moinuddin Quadri's suggestion:</p>

<pre><code>def remember(func):
    def inner(*args, **kwargs):
        args[0].procedure.append({'method': func,
                                  'args': args,
                                  'kwargs': kwargs})
        return func(*args, **kwargs)
    return inner

class process(object):
    def __init__(self):
        self.procedure = []

    @remember
    def MethodA(self, valueA):
        pass

    @remember
    def MethodB(self, valueB):
        pass
</code></pre>

<p>This makes it so that every time each of these decorated methods is called, it will append their arguments to that object's procedure array.  This decorator will fail on non-class methods.</p>

<p>Calling these commands:</p>

<pre><code>p = process()

p.MethodA(1)
p.MethodB(2)
p.MethodA(3)
p.MethodB(4)
print p.procedure
</code></pre>

<p>will result in the following if pretty printed:</p>

<pre><code>[{'args': (&lt;__main__.process object at 0x7f25803d28d0&gt;, 1),
  'kwargs': {},
  'method': &lt;function MethodA at 0x7f25803d4758&gt;},
 {'args': (&lt;__main__.process object at 0x7f25803d28d0&gt;, 2),
  'kwargs': {},
  'method': &lt;function MethodB at 0x7f25803d4848&gt;},
 {'args': (&lt;__main__.process object at 0x7f25803d28d0&gt;, 3),
  'kwargs': {},
  'method': &lt;function MethodA at 0x7f25803d4758&gt;},
 {'args': (&lt;__main__.process object at 0x7f25803d28d0&gt;, 4),
  'kwargs': {},
  'method': &lt;function MethodB at 0x7f25803d4848&gt;}]
</code></pre>

<p>where <code>&lt;__main__.process object at 0x7f25803d28d0&gt;</code> is the process object (<code>self</code>)</p>

<p>Read more about decorators here: <a href="https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators" rel="nofollow">https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators</a></p>

python