Python:使用2to3将Python2转Python3

Python2中的print是一个语句,而Python3中是一个函数

Python2代码 example.py

def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

命令行中执行指令

$ 2to3 -w example.py

Python3代码

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

书写兼容代码

from __future__ import print_function

参考:
https://docs.python.org/2/library/2to3.html

猜你喜欢

转载自blog.csdn.net/mouday/article/details/93506342
今日推荐