In Python, I have a decorator that has to skip any real work if a function is defined locally in the one that calls it. I made a simple testing script:
def fn1():
# @my_decorator will be here
def fn2():
pass
print(fn2)
return fn2
x = fn1()
print(x)
print(x.__module__)
It prints this:
<function fn1.<locals>.fn2 at 0x7fd61bdf3ae8>
<function fn1.<locals>.fn2 at 0x7fd61bdf3ae8>
__main__
As I see, Python sees that the function is defined in a local space (<locals> in the printed text), but I can't see how I can find that bit of data. I walked through the inspect module, and don't see anything similar.
I can't rely on whether the function is in globals or not.
What do I use?