def giveupthefunc():
    frame = inspect.currentframe(1)
code = frame.f_code
globs = frame.f_globals
functype = type(lambda : 0)
funcs = []
for func in gc.get_referrers(code):
    if type(func) is functype:
        if getattr(func, 'func_code', None) is code:
            if getattr(func, 'func_globals', None) is globs:
                funcs.append(func)
if len(funcs) > 1:
    return None
return funcs[0] if funcs else None
Python code to get current function into a variable?