Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>New Solution</strong></p> <p>This is a new that solution detects when the result of the function is used for assignment by examining its own bytecode. There is no bytecode writing done, and it should even be compatible with future versions of Python because it uses the opcode module for definitions.</p> <pre><code>import inspect, dis, opcode def check_status(): try: frame = inspect.currentframe().f_back next_opcode = opcode.opname[ord(frame.f_code.co_code[frame.f_lasti+3])] if next_opcode == "POP_TOP": # or next_opcode == "RETURN_VALUE": # include the above line in the if statement if you consider "return check_status()" to be assignment print "I was not assigned" print "Pretty printer status check 0.02v" print "NOTE: This is so totally not written for giant robots" return finally: del frame # do normal routine info = {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5} return info # no assignment def test1(): check_status() # assignment def test2(): a = check_status() # could be assignment (check above for options) def test3(): return check_status() # assignment def test4(): a = [] a.append(check_status()) return a </code></pre> <p><strong>Solution 1</strong></p> <p>This is the old solution that detects whenever you are calling the function while debugging under python -i or PDB.</p> <pre><code>import inspect def check_status(): frame = inspect.currentframe() try: if frame.f_back.f_code.co_name == "&lt;module&gt;" and frame.f_back.f_code.co_filename == "&lt;stdin&gt;": print "Pretty printer status check 0.02v" print "NOTE: This is so totally not written for giant robots" finally: del frame # do regular stuff return {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5} def test(): check_status() &gt;&gt;&gt; check_status() Pretty printer status check 0.02v NOTE: This is so totally not written for giant robots {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5} &gt;&gt;&gt; a=check_status() Pretty printer status check 0.02v NOTE: This is so totally not written for giant robots &gt;&gt;&gt; a {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5} test() &gt;&gt;&gt; </code></pre>
 

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