1. 程式人生 > >python2.x與python3.x的一些簡單區別

python2.x與python3.x的一些簡單區別

(1)去除print語句,加入print()函式實現相同的功能。同樣的還有 exec語句,已經改為exec()函式 

2.X: print "The answer is", 2*2  

3.X: print("The answer is", 2*2)       

2.X: print x,                              # 使用逗號結尾禁止換行       

3.X: print(x, end=" ")                     # 使用空格代替換行       

2.X: print                                 # 輸出新行       

3.X: print()                               # 輸出新行      

2.X: print >>sys.stderr, "fatal error"       

3.X: print("fatal error", file=sys.stderr)       

2.X: print (x, y)                          # 輸出repr((x, y))      

3.X: print((x, y))                         # 不同於print(x, y)!

(2)迭代器的next()方法改名為__next__(),並增加內建函式next(),用以呼叫迭代器的__next__()方法 

(3)新的super(),可以不再給super()傳引數, 
    >>> class C(object): 
          def __init__(self, a): 
             print('C', a) 
    >>> class D(C): 
          def __init(self, a): 
             super().__init__(a) # 無引數呼叫super() 
    >>> D(8) 
    C 8 
    <__main__.D object at 0x00D7ED90> 

我也是目前只用到了以上這些,平時主要還是使用的python2.7,只是有些資料中使用的是python3.5,所以整理一下以備後用。

原創作者連結為:http://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html