Python:使用2to3将Python2转Python3

简介: 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

            </div>
目录
相关文章
|
4天前
|
XML Shell 数据库
V1.0.0 Python可以做什么
V1.0.0 Python可以做什么
12 0
|
4天前
|
前端开发 应用服务中间件 Apache
Python的CherryPy
Python的CherryPy
40 3
|
6月前
|
C++ 开发者 Python
46 python - self
46 python - self
20 0
|
11月前
|
Python
Python:使用2to3将Python2转Python3
Python:使用2to3将Python2转Python3
35 0
|
Python
python:for-else和while-else应用
python: for-else while-else
946 0
|
移动开发 Python