2013-07-02 108 views
5

我一直认为__file__变量总是给你当前的文件名,但这看起来并不像它所做的那样。因为如果我认为这是真的,我一直在面对一个bug。Python的__file__实际上做了什么?

有人告诉我“__file__是指最后搜索的模块” 这似乎更准确,但我想知道__file__真的应该做什么。

我找不到在Python文档中提到的任何具体内容。 很多地方似乎都提到它,但并不十分清楚。

http://docs.python.org/2/c-api/import.html?highlight=__file__

http://docs.python.org/2/c-api/module.html?highlight=__file__

+8

这可能帮助:http://stackoverflow.com/questions/7116889/python-file-attribute-absolute-or -relative –

+3

*有人告诉我“'__file__'是指最后搜索的模块”*有人错了,恐怕。 –

+0

@MartijnPieters哈,以及我只是用它作为我不清楚'__file__'的例子 – ffledgling

回答

3
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present 
for C modules that are statically linked into the interpreter; for 
extension modules loaded dynamically from a shared library, it is the 
pathname of the shared library file. 

从这里: http://mail.python.org/pipermail/python-dev/2010-February/097461.html

相关问题