引数が渡されずに実行された場所を示す関数where
はどのように定義すればよいですか? すべてのファイルで、〜/アプリ/関数が実行された場所を特定しますか?
a.py:
def where():
return 'the file name where the function was executed'
b.py:
from a import where
if __name__ == '__main__':
print where() # I want where() to return '~/app/b.py' like __file__ in b.py
c.py:あなたがルックアップする必要がある
from a import where
if __name__ == '__main__':
print where() # I want where() to return '~/app/c.py' like __file__ in c.py
あなたはすでに__file__として必要な情報を持っています。なぜそれを返す関数を書く必要がありますか? – kindall
ここをクリックしてください:http://docs.python.org/2/library/inspect.html – StoryTeller
@kindallどこで実行されたのかを知りたいのですが、それを関数本体の変数として使用します。 – walknotes