Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really want to know whether two functions always do the same thing for all inputs, you will have to run them both on all inputs (which will take infinite time), and also intercept all possible side effects (which is effectively impossible).</p> <p>You could of course come up with some heuristics, throwing a set of different values at them that, for your application area, are very likely to generate different outputs if the functions are different. But there's obviously no general-purpose solution to that—otherwise, all unit tests would be generated automatically, saving us all a whole lot of work, right?</p> <hr> <p>Conversely, you might just want to know whether two functions have the exact same implementation. For that, Martijn Pieters's answer is the obvious starting point, and possibly even the ending point (depending on whether you care about closures, globals, etc.).</p> <hr> <p>But what you asked for is something different from either of these; you apparently want to look over the code manually to see "how it works":</p> <blockquote> <p>Another solution would be some method I can run on a function to see what it contains or how it works. So a kind of (lambda x: x).what() that would return how the method works, maybe in a dictionary or something.</p> </blockquote> <p>That function already exists: <a href="http://docs.python.org/3/library/dis.html#dis.dis" rel="noreferrer"><code>dis.dis</code></a>. When you run it on a function, it tells you how that function works. Not in a dictionary (a dictionary of what?) but in a sequence of lines of bytecode for the Python interpreter (which is a relatively simple stack machine with some higher-level stuff added on top, mostly described right there in the <code>dis</code> docs).</p> <p>Or, even more simply, you can get the source with <a href="http://docs.python.org/3/library/inspect.html#inspect.getsource" rel="noreferrer"><code>inspect.getsource</code></a>.</p> <p>Here's what the two look like with your examples:</p> <pre><code>&gt;&gt;&gt; f1 = lambda x: x &gt;&gt;&gt; f2 = lambda y: y &gt;&gt;&gt; def f3(z): ... return z &gt;&gt;&gt; dis.dis(f1) 1 0 LOAD_FAST 0 (x) 3 RETURN_VALUE &gt;&gt;&gt; dis.dis(f2) 1 0 LOAD_FAST 0 (y) 3 RETURN_VALUE &gt;&gt;&gt; dis.dis(f3) 1 0 LOAD_FAST 0 (z) 3 RETURN_VALUE &gt;&gt;&gt; inspect.getsource(f1) 'f1 = lambda x: x\n' &gt;&gt;&gt; inspect.getsource(f2) 'f2 = lambda y: y\n' &gt;&gt;&gt; inspect.getsource(f3) 'def f3(z):\n return z\n' </code></pre> <p>In the first case, you need to know enough about <code>dis</code> to realize that the <code>(x)</code>, etc., are not part of the bytecode, but rather part of the function's list of local names. (This is explained as much in the <code>inspect</code> docs as in the <code>dis</code> docs.) In the second, you need to know enough about Python to realize that the <code>def</code> and the <code>lambda</code> are defining the exact same function. So, either way, there's no way to <em>automate</em> this (or, really, anything much beyond Martijn's answer).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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