Python2.x 中使用 Python3.x 的 print 函数-成都凡恩机器人联盟
如果 Python2.x 版本想使用 Python3.x 的 print 函数,可以导入 __future__ 包,该包禁用 Python2.x 的 print 语句,采用 Python3.x 的 print 函数:
>>> list =["a", "b", "c"] >>> print list # python2.x 的 print 语句 ['a', 'b', 'c'] >>> from __future__ import print_function # 导入 __future__ 包 >>> print list # Python2.x 的 print 语句被禁用,使用报错 File "<stdin>", line 1 print list ^ SyntaxError: invalid syntax >>> print (list) # 使用 Python3.x 的 print 函数 ['a', 'b', 'c'] >>>
Python3.x 与 Python2.x 的许多兼容性设计的功能可以通过 __future__ 这个包来导入。