I'm starting to program in Python 3. And I was wondering if there is any function or method that allows me to know from which place (perhaps, from which line) the function was invoked.
For example, if I have the following code...
1 def foo():
2 print("Hi!")
3
4 def suma(a, b):
5 return a+b
6
7
8 def main():
9 foo()
10 suma(3,6)
11
12
13 if __name__ == "__main__":
14 main()
15 else:
16 print("Function main() not exist")
17
Somehow know that the function:
foo: It is defined in line 1.
It has been called from main on line 9
suma: It is defined in line 4.
It has been called from main on line 10.
Is there some function that does this or maybe something similar?
Maybe something like:
foo.__ code __. co_nlocals
suma.__ code __. co_nlocals
But with the aforementioned.