美文网首页Python
from __future__ import print_fun

from __future__ import print_fun

作者: Seafairy | 来源:发表于2019-04-11 00:09 被阅读0次

在一些代码中看到‘from __future__ import print_function’这个语句,经查找资料,此句的用途及用法记录如下:

用途:把python新版本中print_function函数的特性导入到当前版本。如,在python2环境下,使用此句,则可以超前使用python3的print函数了。

(以上参考:https://zhuanlan.zhihu.com/p/28641474 )

用法:只能放置于当前运行代码片段的最开头。否则会报错如下 “ SyntaxError: from __future__ imports must occur at the beginning of the file”:

【用如下例子解释为什么是当前运行的代码片段:

(这样import就不会报错,也是奇怪。。。)

相关文章

网友评论

    本文标题:from __future__ import print_fun

    本文链接:https://www.haomeiwen.com/subject/gxaaiqtx.html