Python 2.x版本和Python3.x版本的不同

在新的Python版本中,Python3.x虽然在主要的核心语言和模块等方面没有打的改变,但是Python3.x与2.x还是有很大区别的。而且Python3.x与2.x并不兼容。
比如说HTTPServer这个模块在3.x中它存放在http.server中,但是在2.x中并没有这个model。在2.x中它被放在BaseHTTPServer中。
因此,我们可以同过dir()函数查看一个内置模块的属性列表和文档字符串。
E:html5>python
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import BaseHTTPServer
>>> dir(BaseHTTPServer)
['BaseHTTPRequestHandler', 'DEFAULT_ERROR_CONTENT_TYPE', 'DEFAULT_ERROR_MESSAGE'
, 'HTTPServer', 'SocketServer', '__all__', '__builtins__', '__doc__', '__file__'
, '__name__', '__package__', '__version__', '_quote_html', 'catch_warnings', 'fi
lterwarnings', 'mimetools', 'socket', 'sys', 'test', 'time']
>>> dir(CGIHTTPServer)
['BaseHTTPServer', 'CGIHTTPRequestHandler', 'SimpleHTTPServer', '__all__', '__bu
iltins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_ur
l_collapse_path', 'copy', 'executable', 'nobody', 'nobody_uid', 'os', 'select',
'sys', 'test', 'urllib']
dir函数会简单的返回一个列表,其中包含啦带属性对象的所有属性字符串名称,这是一种在交互提示符下唤醒对模块记忆的便捷方式。
原文地址:https://www.cnblogs.com/Sweethoney/p/4964222.html