1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonコードの任意の場所で関数呼び出しの履歴を表示するには

Last updated at Posted at 2025-01-27

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()

関連情報:

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?