我使用Python 2.6.5版本,可以在不调用from __future__ import with_statement
的情况下使用with
语句。如何判断哪个版本的Python支持with
而无需从__future__
中具体导入它?
我使用Python 2.6.5版本,可以在不调用from __future__ import with_statement
的情况下使用with
语句。如何判断哪个版本的Python支持with
而无需从__future__
中具体导入它?
__future__
特性是自我描述的。请尝试这样做:
>>> from __future__ import with_statement
>>> with_statement.getOptionalRelease()
(2, 5, 0, 'alpha', 1)
>>> with_statement.getMandatoryRelease()
(2, 6, 0, 'alpha', 0)
这些分别表示支持from __future__ import with_statement
的第一个版本和支持它而不使用from __future__
的第一个版本。
另外,请阅读此内容:
>>> import __future__
>>> help(__future__)
from __future__ import with_statement
即可。对于更新的版本,它将被忽略。文档中的内容:
New in version 2.5.