常见的Python 2 和 3 差别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/soonfly/article/details/78391285

Python2.7

print "hello"

hello

Python3.0

print "hello"

  File "tmp.py", line 1
    print "hello"
                ^
SyntaxError: invalid syntax

正确应为:
Python3.0

print("hello")

hello

Python2.7

print 1 / 2

0

Python3.0

print(1 / 2)

0.5

Python2.7

print {}.keys()

[]

Python3.0

print({}.keys())

<dict_keys object at 0x385f50>

Python2.7

print None < None

False

Python3.0

print(None < None)

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    print(None < None)
TypeError: unorderable types: NoneType() < NoneType()

Python2.7

print 055

45

Python3.0

print(055)

  File "tmp.py", line 1
    print(055)
            ^
SyntaxError: invalid token

正确做法:
Python3.0

print(0o55)

45

Python2.7

print long(987654321*987654321)

975461057789971041

Python3.0

print(long(987654321*987654321))

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    print(long(987654321*987654321))
NameError: name 'long' is not defined

正确做法:
Python3.0

print(int(987654321*987654321))

975461057789971041

Python2.7

print u"\u0024"

$

Python3.0

print(u"\u0024")

  File "tmp.py", line 1
    print(u"\u0024")
                  ^
SyntaxError: invalid syntax

正确做法
Python3.0

print("\u0024")

$

Python2.7

if (isinstance("helloworld", basestring)):
    print "True"

True

Python3.0

if (isinstance("helloworld", basestring)):
  print("True")


Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    if (isinstance("string", basestring)):
NameError: name 'basestring' is not defined

正确应该是

Python3.0

if (isinstance("string", str)):
  print("True")

True

Python2.7

True = "True"

(no output)

Python3.0

True = "True"

  File "tmp.py", line 1
    True = "True"
SyntaxError: assignment to keyword

Python2.7

False = "False"

(no output)

Python3.0

False = "False"

  File "tmp.py", line 1
    False = "False"
SyntaxError: assignment to keyword

Python2.7

try:
  print 1+1
except ValueError, err:
  print err

2

Python3.0

try:
  print(1+1)
except ValueError, err:
  pass

  File "tmp.py", line 3
    except ValueError, err:
                     ^
SyntaxError: invalid syntax

正确应该是:

Python3.0

try:
  print(1+1)
except ValueError as err:
  print(err)

2

Python2.7

print [x for x in 1, 2, 3]

[1, 2, 3]

Python3.0

print([x for x in 1, 2, 3])

  File "tmp.py", line 1
    print([x for x in 1, 2, 3])
                       ^
SyntaxError: invalid syntax

正确应该是:

Python3.0

print([x for x in (1, 2, 3)])

[1, 2, 3]

Python2.7

print ''.join([`x` for x in (1,2,3)])

123

Python3.0

print(''.join([`x` for x in (1,2,3)]))

  File "tmp.py", line 1
    print(''.join([`x` for x in (1,2,3)]))
                   ^
SyntaxError: invalid syntax

正确做法是:

Python3.0

print(''.join([repr(x) for x in (1,2,3)]))

123

Python2.7

print 1 <> 2

True

Python3.0

print(1 <> 2)

  File "tmp.py", line 1
    print(1 <> 2)
             ^
SyntaxError: invalid syntax

正确做法
Python3.0

print(1 != 2)

True

Python2.7

exec = "exec"

  File "tmp.py", line 1
    exec = "exec"
         ^
SyntaxError: invalid syntax

Python3.0

exec = "exec"

(no output)

Python2.7

print buffer("Hello world")

Hello world

Python3.0

print(buffer("Hello world", 6, 5))

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    print(buffer("Hello world", 6, 5))
NameError: name 'buffer' is not defined

正确做法:
Python3.0

print(repr(memoryview(b"Hello world")))

<memory at 0x3c3660>

Python2.7

print {"a":1}.has_key("a")

True

Python3.0

print({"a":1}.has_key("a"))

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    print({"a":1}.has_key("a"))
AttributeError: 'dict' object has no attribute 'has_key'

正确做法:

Python3.0

print("a" in {"a":1})

True

Python3.0

import _winreg

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import _winreg
ImportError: No module named _winreg

Python2.7

import ConfigParser

(no output)

Python3.0

import ConfigParser

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import ConfigParser
ImportError: No module named ConfigParser

正确做法:

Python3.0

import configparser

(no output)

Python2.7

import copy_reg

(no output)

Python3.0

import copy_reg

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import copy_reg
ImportError: No module named copy_reg

正确做法:

Python3.0

import copyreg

(no output)

Python2.7

import Queue

(no output)

Python3.0

import Queue

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import Queue
ImportError: No module named Queue

正确做法
Python3.0

import queue

(no output)

Python2.7

import SocketServer

(no output)

Python3.0

import SocketServer

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import SocketServer
ImportError: No module named SocketServer

正确做法:
Python3.0

import socketserver

(no output)

Python2.7

import markupbase

(no output)

Python3.0

import markupbase

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import markupbase
ImportError: No module named markupbase

正确做法:

Python3.0

import _markupbase

(no output)

Python2.7

import repr

(no output)

Python3.0

import repr

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import repr
ImportError: No module named repr

正确做法:

Python3.0

import reprlib

(no output)

Python2.7

import test.test_support

(no output)

Python3.0

import test.test_support

Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import test.test_support
ImportError: No module named test_support

正确做法:

Python3.0

import test.support

(no output)

Python2.7

import string
print string.letters

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Python3.0

import string
string.letters

Traceback (most recent call last):
  File "tmp.py", line 2, in <module>
    string.letters
AttributeError: 'module' object has no attribute 'letters'

正确做法:

Python3.0

import string
print(string.ascii_letters)

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Python2.7

import string
print string.uppercase

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Python3.0

import string
string.uppercase

Traceback (most recent call last):
  File "tmp.py", line 2, in <module>
    string.uppercase
AttributeError: 'module' object has no attribute 'uppercase'

正确做法:

Python3.0

import string
print(string.ascii_uppercase)

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Python2.7

import string
print string.lowercase

abcdefghijklmnopqrstuvwxyz

Python3.0

import string
string.lowercase

Traceback (most recent call last):
  File "tmp.py", line 2, in <module>
    string.lowercase
AttributeError: 'module' object has no attribute 'lowercase'

正确做法:

Python3.0

import string
print(string.ascii_lowercase)

abcdefghijklmnopqrstuvwxyz

猜你喜欢

转载自blog.csdn.net/soonfly/article/details/78391285