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

展开阅读全文

没有更多推荐了,返回首页