原
from __future__ import print_function 用法
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaotao_1/article/details/79460365 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div id="content_views" class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p> 在开头加上<code>from __future__ import print_function</code>这句之后,即使在python2.X,使用print就得像python3.X那样加括号使用。python2.X中print不需要括号,而在python3.X中则需要。</p>
# python2.7
print "Hello world"
# python3
print("Hello world")
- 1
- 2
- 3
- 4
- 5
如果某个版本中出现了某个新的功能特性,而且这个特性和当前版本中使用的不兼容,也就是它在该版本中不是语言标准,那么我如果想要使用的话就需要从future模块导入。
其他例子:
from __future__ import division
,
from __future__ import absolute_import
,
from __future__ import with_statement
。等等
加上这些,如果你的python版本是python2.X,你也得按照python3.X那样使用这些函数。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaotao_1/article/details/79460365 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div id="content_views" class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p> 在开头加上<code>from __future__ import print_function</code>这句之后,即使在python2.X,使用print就得像python3.X那样加括号使用。python2.X中print不需要括号,而在python3.X中则需要。</p>