luc*_*one 4 python import module double-underscore
我是Python新手,已经开始研究其他人编写的代码了.
在从Pypi下载的软件包源代码中,我注意到了使用
import __module__
Run Code Online (Sandbox Code Playgroud)
使用src包文件夹中定义的函数和类.
这是常见做法吗?我实际上无法理解这种语法,你可以向我解释一下或者给我一些参考吗?
这是一些内置对象的python约定.来自PEP8:
__double_leading_and_trailing_underscore__:生成在用户控制的命名空间中的"魔术"对象或属性.例如__init__,__import__或__file__.不要发明这样的名字; 只记录使用它们.
但最终它不是一种理解与否的"语法",__module__只是一个带有下划线的名称.它与完全无关,与之完全无关module.
一个示例向您展示它只是一个名称:
>>> __foo__ = 42
>>> print foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> print _foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_foo' is not defined
>>> print __foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__foo' is not defined
>>> print __foo__
42
>>> type(__foo__)
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
没有什么本质上特别的.
如果没有关于你在哪里看到这个的更多信息,很难说出作者的意图是什么.要么他们正在导入一些python内置from __future__ import...函数(例如),要么他们忽略了PEP8并且只是用这种风格命名了一些对象,因为他们认为它看起来很酷或更重要.
| 归档时间: |
|
| 查看次数: |
1250 次 |
| 最近记录: |