programmatically determine if an evaluation of a function is being assigned?
<p>I would like to do this:</p>

<pre><code>def foo():
    if &lt;a magical condition&gt;:
        return x
    else:
        poof()

# or...
def foo():
    x = &lt;a magical object&gt;
    return x

def poof():
    print 'poof!'

bar = foo()  # bar points to &lt;a magical object&gt; but poof() is not called

foo() # prints 'poof!'
</code></pre>

<p>I guess it comes down to what the circumstanses are when the returned object's <code>__del__</code> method is called.  But maybe there is a better way.  Like if the function itself knew it's returned value was being assigned.  I guess I'm worried about relying on the timing of the garbage collection.  Also I don't like that global <code>at_end_of_program</code> flag.</p>

<p>My solution:</p>

<pre><code>class Magic:
    def __del__(s):
        poof()

def foo():
    x = Magic()
    return x

def poof():
    if not at_end_of_program:
        print 'poof!'

bar = foo()  # No poof.

foo() # prints 'poof!'
</code></pre>
