python的__future__

374 阅读1分钟
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

使用的原因

python2.x 和 python3.x 在诸如Unicode字符串, print输出, 除法方面是不同的

为了能够在低版本中使用python新版本的语法特性, 可以使用__future__中对应的对象

譬如, python 2.x中print不加括号, 导入print_function后, 则使用python2进行运行代码也要求代码中print函数要跟括号

__future__中常用的语法特性

  • absolute_import: 涉及到python2和python3包搜查顺序的问题
  • division: python2中整数除以整数还是整数
  • print_function: 括号的问题
  • unicode_literals: 将python2中的字符串显式变为unicode字符