2
只是好奇,我想from __future__ import *
,但我收到此錯誤:進口今後所有功能
File "<stdin>", line 1
SyntaxError: future feature * is not defined
嗯,這是有道理的。一個__future__
導入是有點特別,不遵循正常的規則,但它讓我想到:如何可以我導入所有未來的功能?
只是好奇,我想from __future__ import *
,但我收到此錯誤:進口今後所有功能
File "<stdin>", line 1
SyntaxError: future feature * is not defined
嗯,這是有道理的。一個__future__
導入是有點特別,不遵循正常的規則,但它讓我想到:如何可以我導入所有未來的功能?
你不能,而且這是通過設計。這是因爲將來可能會添加更多__future__
功能,並且這些功能可能會破壞您的代碼。
想象一下,在2.x中,唯一的__future__
功能是division
。然後在2.y中引入一個新的__future__
功能print_function
。霎時間我的代碼已經突破:
from __future__ import *
print "Hello, World!"
您可以,然而,進口__future__
,並檢查其內容:
>>> import __future__
>>> [x for x in dir(__future__) if x.islower() and x[0] != '_']
['absolute_import', 'all_feature_names', 'division', 'generator_stop', 'generators', 'nested_scopes', 'print_function', 'unicode_literals', 'with_statement']
注意,這些都沒有的功能,你不應該嘗試導入它們。他們反而描述了哪些功能可用,以及它們來自哪個版本。