Check if there's something "waiting for" the return value of a function
<p>I'm wondering if anyone can think up a way to check if a function needs to return a meaningful value in Python. That is, to check <em>whether the return value will be used for anything</em>. I'm guessing the answer is no, and it is better to restructure my program flow. The function in question pulls its return values from a network socket. If the return value is not going to get used, I don't want to waste the resources fetching the result.</p>

<p>I tried already to use tracebacks to discover the calling line, but that didn't work. Here's an example of what I had in mind:</p>

<pre><code>&gt;&gt;&gt; def func():
...  print should_return()
...
&gt;&gt;&gt; func()
False
&gt;&gt;&gt; ret = func()
True
</code></pre>

<p>The function "knows" that its return value is being assigned.</p>

<p>Here is my current workaround:</p>

<pre><code>&gt;&gt;&gt; def func(**kwargs):
...   should_return = kwargs.pop('_wait', False)
...   print should_return
...
&gt;&gt;&gt; func()
False
&gt;&gt;&gt; ret = func(_wait=True)
True
</code></pre>
