从__future__导入with_statement需要哪个python版本?

mlz*_*boy 25 python python-import

使用python 2.6.5,我可以在with不调用的情况下使用该语句from __future__ import with_statement.如何在with不专门导入Python的情况下判断哪个版本的Python支持__future__

Mar*_*nen 48

__future__功能是自我记录.试试这个:

>>> from __future__ import with_statement
>>> with_statement.getOptionalRelease()
(2, 5, 0, 'alpha', 1)
>>> with_statement.getMandatoryRelease()
(2, 6, 0, 'alpha', 0)
Run Code Online (Sandbox Code Playgroud)

这些分别表示第一个版本支持from __future__ import with_statement和第一个版本支持它而不使用from __future__.

另外,请阅读:

>>> import __future__
>>> help(__future__)
Run Code Online (Sandbox Code Playgroud)


And*_*Dog 16

你只需要在Python 2.5中使用它.旧版本的(<= 2.4)不支持它和新版本(> = 2.6)有它默认启用.

因此,如果你想支持Python> = 2.5,你可以简单地把它from __future__ import with_statement放在开头.对于较新的版本,它将被忽略.