我应该如何定义一个函数,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 2013-04-30 17:43:32
看看这里:http://docs.python.org/2/library/inspect.html – StoryTeller 2013-04-30 17:43:32
@kindall我希望where()知道它在哪里执行,并将它用作其函数体中的变量。 – walknotes 2017-09-17 04:07:39