How to reconstruct Python function from memory address?
Question: 
    >> def spam():
    ...   print("top secret function")
    ... 
    >>> print(spam)
    <function spam at 0x7feccc97fb78>
    >>> spam = "spam"
    

So I lose the reference to `spam` function. Can I get it back from that memory
address: 0x7feccc97fb78?

    
    
    >>> orig_spam_function = get_orig_func_from_memory_address("0x7feccc97fb78")
    

**Edit** (_responding to thefourtheye_):

Sorry for the lousy question, consider this case:

    
    
    >>> from collections import defaultdict
    >>> d = defaultdict(spam)
    >>> d
    defaultdict(<function spam at 0x7f597572c270>, {})
    

So the function is not garbaged collected yet. Can I recover it? Of course, in
this case, you can use `default_factory` attribute.

    
    
    >>> d.default_factory
    <function spam at 0x7f597572c270>
    

But imagine `defaultdict` without `default_factory` attribute.


Answer: When you assign

    
    
    spam = "spam"
    

the last reference to the `spam` function is gone, the reference count becomes
0 and that will be garbage collected later. So, there is no way, we can get it
back. We can check that with this program

    
    
    def spam():
        print("top secret function")
    import sys
    print id(spam), sys.getrefcount(spam)
    spam = "spam"
    print id(spam), sys.getrefcount(spam)
    

**Output on my machine**

    
    
    140068817052928 2
    140068817075440 12
    

The actual address of `spam` was different than the one which we see after the
assignment statement. So, it is pointing to a different object now. But,
originally, the reference count is 1 (`getrefcount` will always be [one higher
than the actual
count](http://docs.python.org/2/library/sys.html#sys.getrefcount)). When we
reassign `spam`, now no one is actually pointing to that function. So, it will
be ready for garbage collection.

