Get the caller of a function in Python

Thing you want to do

I want to get the file name / function that called the function being processed.

Method

Use inspect.

How to do

caller_file.py


import callee_file


def main():
    callee_file.callee_function()


main()

callee_file.py


import inspect

def callee_function():
    # print(inspect.stack())
    print(inspect.stack()[1].filename)
    print(inspect.stack()[1].function)

Calls are stored in inspect.stack in order of slowest call. If you uncomment the 4th line, the following array will be output.

[
  FrameInfo(frame=<frame at 0x7fa68460f050, file '/path-to-dir/callee_file.py', line 4, code callee_function>, filename='/path-to-dir/callee_file.py', lineno=4, function='callee_function', code_context=['    print(inspect.stack())\n'], index=0),
  FrameInfo(frame=<frame at 0x10cc17650, file 'caller_file.py', line 5, code main>, filename='caller_file.py', lineno=5, function='main', code_context=['    callee_file.callee_function()\n'], index=0),
  FrameInfo(frame=<frame at 0x10ca61450, file 'caller_file.py', line 8, code <module>>, filename='caller_file.py', lineno=8, function='<module>', code_context=['main()\n'], index=0)
]

The first is the information on print (inspect.stack ()) on the 4th line of callee_file.py, The second is the information of callee_file.callee_function () on the 5th line of caller_file.py. The third is the information on main () on line 8 of caller_file.py.

Since we want the information of the previous function call, we need ʻinspect.stack () [1] to get the information of the second array. The properties of ʻinspect.stack () [1] are as follows, but this time it is good if you can get the file name and function name. Use ʻinspect.stack () [1] .filename, and ʻinspect.stack () [1] .function. By the way, you can also get the array number, so you can use ʻinspect.stack () [1] [1], and ʻinspect.stack () [1] [3].

reference

https://stackoverflow.com/questions/900392/getting-the-caller-function-name-inside-another-function-in-python

Recommended Posts

Get the caller of a function in Python
Get the number of specific elements in a python list
A function that measures the processing time of a method in python
Create a function to get the contents of the database in Go
Get the number of readers of a treatise on Mendeley in Python
Draw a graph of a quadratic function in Python
[Python] Get the files in a folder with Python
Make a copy of the list in Python
Output in the form of a python array
Get a glimpse of machine learning in Python
Create a function in Python
Get a capture of the entire web page in Selenium Python VBA
Get a datetime instance at any time of the day in Python
How to get the number of digits in Python
[python] Get the list of classes defined in the module
Get the size (number of elements) of UnionFind in Python
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
What does the last () in a function mean in Python?
Get the desktop path in Python
Get the script path in Python
[Python] Make the function a lambda function
Get the desktop path in Python
Get the host name in Python
How to get a list of files in the same directory with python
Try to get the function list of Python> os package
Find out the apparent width of a string in python
Have the equation graph of the linear function drawn in Python
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
A memo of writing a basic function in Python using recursion
Find the eigenvalues of a real symmetric matrix in Python
Get the index of each element of the confusion matrix in Python
Check the behavior of destructor in Python
Write the test in a python docstring
OR the List in Python (zip function)
[Python3] Rewrite the code object of the function
Run the Python interpreter in a script
The result of installing python in Anaconda
How to get a stacktrace in python
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
Get a token for conoha in python
In search of the fastest FizzBuzz in Python
[Python] Get the character code of the file
Get the filename of a directory (glob)
The story of blackjack A processing (python)
Get the EDINET code list in Python
[Python] Get a list of folders only
Get rid of DICOM images in Python
How to determine the existence of a selenium element in Python
Try transcribing the probability mass function of the binomial distribution in Python
How to check the memory size of a variable in Python
Read the standard output of a subprocess line by line in Python
How to check the memory size of a dictionary in Python
[Python] Get the update date of a news article from HTML
[python] Get the rank of the values in List in ascending / descending order
[Python3] Define a decorator to measure the execution time of a function
How to get the vertex coordinates of a feature in ArcPy