【问题标题】:What does Python's __file__ actually do? [duplicate]Python 的 __file__ 实际上做了什么? [复制]
【发布时间】:2013-07-02 14:53:20
【问题描述】:

我一直认为__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__

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/7116889/…
  • 有人告诉我“__file__ 指的是最后搜索的模块” 恐怕有人错了。
  • @MartijnPieters 哈,我只是用它作为一个例子来说明我对__file__987654330@ 的了解程度

标签: python


【解决方案1】:
 __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

【讨论】:

    猜你喜欢
    • 2014-07-14
    • 2013-01-10
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2013-06-02
    • 2020-11-21
    • 2015-02-26
    • 2017-06-29
    相关资源
    最近更新 更多