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,或者使用精確除法。
以下是在python2.7.12交互模式下運行:
>>> print 'Bunny' Bunny >>> 3/2 1 >>> from __future__ import print_function, division >>> print('Huang') Huang >>> 3/2 1.5我們可以看到,在我們沒有導入模塊時,print不用加括號,導入后,python2就可以使用python3中print的特性;同樣,當我們沒有導入該模塊時,/操作符執行的是截斷除法,當我們導入后,/執行的是精確除法。
總結
以上是生活随笔為你收集整理的python观察日志(part10)--__future__ 模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TP-Link TL-WDR8600 无
- 下一篇: 菜鸟裹裹怎么添加亲友