Help us understand the problem. What is going on with this article?

pythonで実行中のファイル、関数、行番号を取得する

More than 3 years have passed since last update.

python3でエラーになるので
frame = inspect.currentframe(depth + 1)から
frame = inspect.currentframe().f_backに修正

import inspect
import os

def location(depth=0):
  frame = inspect.currentframe().f_back
  return os.path.basename(frame.f_code.co_filename), frame.f_code.co_name, frame.f_lineno

def func1():
  print(location())

def main():
  print(location())
  func1()

if __name__ == '__main__':
  main()
  exit(0)
('a.py', 'main', 13)
('a.py', 'func1', 10)

参考

Python で現在のファイル名と行番号を調べる。 - kなんとかの日記 http://d.hatena.ne.jp/kwatch/20100410/1270851205

Python Tips:現在の関数の名前や引数を取得したい - Life with Python http://www.lifewithpython.com/2015/11/python-get-function-name.html

実行中の関数・メソッド名を取得したい - Qiita http://qiita.com/megmogmog1965/items/0b4ea3d58e34f1854158

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした