【问题标题】:Which python version needs from __future__ import with_statement?__future__ import with_statement 需要哪个 python 版本?
【发布时间】:2010-09-25 00:26:56
【问题描述】:

使用python 2.6.5,我可以使用with 语句而不调用from __future__ import with_statement。我如何知道哪个版本的 Python 支持with 而无需专门从__future__ 导入它?

【问题讨论】:

    标签: python python-import


    【解决方案1】:

    __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__)
    

    【讨论】:

      【解决方案2】:

      您只需要在 Python 2.5 中使用它。旧版本 (= 2.6) 默认启用它。

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

      【讨论】:

        【解决方案3】:

        来自文档:

        New in version 2.5.
        

        【讨论】:

          猜你喜欢
          • 2015-03-15
          • 1970-01-01
          • 2011-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-21
          • 2011-08-30
          • 1970-01-01
          相关资源
          最近更新 更多