Pythonコードの任意の場所で、現在行での関数呼び出しの履歴を表示する方法:
下記のコードを任意の場所に入れておきます。
Console
print("DEBUG: FUNC-CALL")
import inspect
for s_i in inspect.stack(): print(f"{s_i.lineno:5d}L, {s_i.function} @ {s_i.filename}")
出力結果の例は、
結果
DEBUG: FUNC-CALL
8L, test_function2 @ C:\...\test.py
10L, test_function1 @ C:\...\test.py
13L, <module> @ C:\...\test.py
サンプルコード全体は、
Console
def test_function2():
print("DEBUG: FUNC-CALL")
import inspect
for s_i in inspect.stack(): print(f"{s_i.lineno:5d}L, {s_i.function} @ {s_i.filename}")
def test_function1():
test_function2()
# メイン処理
if __name__ == "__main__":
test_function1()
関連情報: