You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1.6 KiB

title date updated tags categories keywords description top_img comments cover toc toc_number toc_style_simple copyright copyright_author copyright_author_href copyright_url copyright_info mathjax katex highlight_shrink aside
零碎的小问题 2021-08-16 17:40:28 2022-08-11 09:33:52 <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>

python中的__future__包

__future__是py2的概念,对应于py2,py3就是future,这是为了在是py2的时候还能用到一些新版本的特性而做成的包。意思禁用py2的import特性改成用py3的import的特性。

  • from future import absolute_import的作用:
    这是相对引用和绝对引用的概念,从相对导入变成绝对导入,大概意指你每次用import X时候都是在sys.path(即python的搜索模块的路径集)里面找X包。只要知道py3的import用法,加上这句话py2时候也能按照py3的import语法去正常使用。

  • from future import division的作用: division(精确除法),当我们没有在程序中导入该特征时,"/"操作符执行的是截断除法(Truncating Division), 当我们导入精确除法之后,"/"执行的是精确除法,如下所示:

>>> 3/4
0
>>> from __future__ import division
>>> 3/4
0.75

# 导入精确除法后,若要执行截断除法,可以使用"//"操作符:
>>> 3//4
0
>>>
  • from future import print_function的用法:
    超前使用python3的print函数。
# 在python2.x的环境是使用下面语句
from __future__ import print_function
print('you are good')   # 语法检查通过
print 'you are good'    # 语法检查失败

未完待续