天天看點

python觀察日志(part10)--__future__ 子產品

學習筆記,有錯必糾

​__future__​

​子產品

我們利用官方文檔學習一下​

​__future__​

​子產品:

To avoid confusing existing tools that analyze import statements and expect to find the modules they’re importing.

To ensure that future statements run under releases prior to 2.1 at least yield runtime exceptions

To document when incompatible changes were introduced, and when they will be — or were — made mandatory. This is a form of executable documentation, and can be inspected programmatically via importing ​

​__future__​

​ and examining its contents.

是以說,如果某個版本(比如python3.5)中存在某個新的功能特性(比如精确除法),而這個特性和目前版本(比如python2.7)不相容,也就是它在該版本中不是語言标準,那麼我如果想要使用該特性的話,就需要從​

​__future__​

​子產品中導入。

python實作

我們可以利用​

​__future__​

​ 子產品,在python2.X中,像python3.X那樣,使用加括号的print,或者使用精确除法。

>>> print 'Bunny'
Bunny
>>> 3/2
1
>>> from __future__ import print_function, division
>>> print('Huang')
Huang
>>> 3/2
1.5