計算機的知識太多了,不少東西就是一個使用過程當中詳細積累的過程。最近遇到了一個好久關於future的問題,踩了坑,這裏就作個筆記,省得後續再犯相似錯誤。python
future的做用:把下一個新版本的特性導入到當前版本,因而咱們就能夠在當前版本中測試一些新版本的特性。說的通俗一點,就是你不用更新python的版本,直接加這個模塊,就能夠使用python新版本的功能。 下面咱們用幾個例子來講明它的用法:git
python 2.x print不是一個函數,不能使用help. python3.x print是一個函數,能夠使用help.這個時候,就能夠看一下future的好處了:python3.x
代碼:app
# python2 #from __future__ import absolute_import, division, print_function #print(3/5) #print(3.0/5) #print(3//5) help(print)
運行結果:函數
➜ future git:(master) ✗ python future.py File "future.py", line 8 help(print) ^ SyntaxError: invalid syntax
報錯了,緣由就是python2 不支持這個語法。測試
上面只須要把第二行的註釋打開:ui
1 # python2 2 from __future__ import absolute_import, division, print_function 3 4 5 #print(3/5) 6 #print(3.0/5) 7 #print(3//5) 8 help(print)
結果以下,就對了:spa
Help on built-in function print in module __builtin__: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline.
另一個例子:是關於除法的:code
# python2 #from __future__ import absolute_import, division, print_function print(3/5) print(3.0/5) print(3//5) #help(print)
結果:blog
➜ future git:(master) ✗ python future.py 0 0.6 0
把編譯宏打開,運算結果:
➜ future git:(master) ✗ python future.py 0.6 0.6 0
看看,python3.x的語法能夠使用了。
有了這兩個例子,估計你對future的用法就清晰了吧。