美文网首页
python的future模块:实现python新旧版本的兼容性

python的future模块:实现python新旧版本的兼容性

作者: 烂笔头2020 | 来源:发表于2020-06-12 11:10 被阅读0次

如果某个版本中出现了某个新的功能特性,而且这个特性和当前版本中使用的不兼容,也就是它在该版本中不是语言标准,那么我如果想要使用的话就需要从future模块导入。

# python2.7
print "Hello world"

# python3
print("Hello world")

在开头加上from future import print_function这句之后,即使在python2.X,使用print就得像python3.X那样加括号使用。python2.X中print不需要括号,而在python3.X中则需要。

其他例子:
    from future import division ,
    from future import absolute_import ,
    from future import with_statement 。等等
  加上这些,如果你的python版本是python2.X,你也得按照python3.X那样使用这些函数。

相关文章

网友评论

      本文标题:python的future模块:实现python新旧版本的兼容性

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