哎一古,python3 和 python2为什么这么多不一样的地方,码一波碰到的不一样的地方,以下适用于0基础刚接触python的小白,比如我。
非常棒的博客: http://blog.csdn.net/samxx8/article/details/21535901
1. python2:print "xxx"
python3:print("xxx")
2. python2:sys.maxint
python3:
sys.maxsize
由于长整型和整型被整合在一起了,sys.maxint
常量不再精确。但是因为这个值对于检测特定平台的能力还是有用处的,所以它被Python 3保留,并且重命名为sys.maxsize
。
Notes | Python 2 | Python 3 |
---|---|---|
① | from sys importmaxint |
from sys importmaxsize |
② | a_function(sys.maxint) |
a_function(sys.maxsize) |
maxint
变成了maxsize
。- 所有的
sys.maxint
都变成了sys.maxsize
。
3. 当碰到这种报错的时候:TabError: inconsistent use of tabs and spaces in indentation
原因竟然是缩进不可用tab键敲出来的缩进,要用空格一个个敲出来,amazing
4. python2:import urlib2
python3:import urllib.request
5. python2:import 默认相对路径,python2缺省会搜索上一级目录、上上级目录 e.g. import releases
python3:import 默认绝对路径, e.g. from mlab import releases
某些情况 from xxx.yyy import zzz 看所属文件夹
6. python2:itertools.count(start).next()
python3:next(itertools.count(start))