python14

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__ 这个包来导入。

 
posted on 2022-12-16 12:08  学习的CYT  阅读(25)  评论(0)    收藏  举报