Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get the values of the locals of a function after it has been executed?
    text
    copied!<p>Suppose I have a function like <code>f(a, b, c=None)</code>. The aim is to call the function like <code>f(*args, **kwargs)</code>, and then construct a new set of args and kwargs such that:</p> <ol> <li>If the function had default values, I should be able to acquire their values. For example, if I call it like <code>f(1, 2)</code>, I should be able to get the tuple <code>(1, 2, None)</code> and/or the dictionary <code>{'c': None}</code>.</li> <li>If the value of any of the arguments was modified inside the function, get the new value. For example, if I call it like <code>f(1, 100000, 3)</code> and the function does <code>if b &gt; 500: b = 5</code> modifying the local variable, I should be able to get the the tuple <code>(1, 5, 3)</code>.</li> </ol> <p>The aim here is to create a a decorator that finishes the job of a function. The original function acts as a preamble setting up the data for the actual execution, and the decorator finishes the job.</p> <p>Edit: I'm adding an example of what I'm trying to do. It's a module for making proxies for other classes.</p> <p><pre><code> class Spam(object): """A fictional class that we'll make a proxy for""" def eggs(self, start, stop, step): """A fictional method""" return range(start, stop, step)</p> <p>class ProxyForSpam(clsproxy.Proxy): proxy_for = Spam @clsproxy.signature_preamble def eggs(self, start, stop, step=1): start = max(0, start) stop = min(100, stop)</p> <p></pre></code></p> <p>And then, we'll have that:</p> <p><code>ProxyForSpam().eggs(-10, 200) -&gt; Spam().eggs(0, 100, 1)</code></p> <p><code>ProxyForSpam().eggs(3, 4) -&gt; Spam().eggs(3, 4, 1)</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload