[英]Python os.path is ntpath, how?
Can someone tell me how Python "aliases" os.path
to ntpath
? 有人能告诉我Python如何“别名”
os.path
到ntpath
吗?
>>> import os.path
>>> os.path
<module 'ntpath' from 'C:\Python26\lib\ntpath.pyc'>
>>>
Look at os.py , lines 55-67: 看一下os.py ,第55-67行:
elif 'nt' in _names:
name = 'nt'
linesep = '\r\n'
from nt import *
try:
from nt import _exit
except ImportError:
pass
import ntpath as path
import nt
__all__.extend(_get_exports_list(nt))
del nt
The import ntpath as path
is the specific statement that causes os.path
to be ntpath
on your platforms (doubtlessly Windows). import ntpath as path
的import ntpath as path
是特定的语句,它导致os.path
成为您平台上的ntpath
(无疑是Windows)。
>>> import os as my_aliased_module
>>> my_aliased_module
<module 'os' from 'C:\Program Files\Python 2.6\lib\os.pyc'>
EDIT: And since import
is a simple statement in Python, you can do neat stuff like: 编辑:因为
import
是Python中的一个简单的声明,你可以做一些整洁的东西,如:
import sys
if sys.platform == 'win32':
import windows_module as my_module
else:
import unix_module as my_module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.