Python:2 to 3でPython 2をPython 3に回す

2222 ワード

Python 2のprintは文で、Python 3は関数です.
Python 2コードpy
def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

コマンドライン実行命令
$ 2to3 -w example.py

Python 3コード
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