27

Using python 2.6.5, I can use the with statement without calling from __future__ import with_statement. How can I tell which version of Python supports with without specifically importing it from __future__?

CharlesB
  • 80,832
  • 27
  • 184
  • 208
mlzboy
  • 13,836
  • 23
  • 73
  • 97

3 Answers3

51

__future__ features are self-documenting. Try this:

>>> from __future__ import with_statement
>>> with_statement.getOptionalRelease()
(2, 5, 0, 'alpha', 1)
>>> with_statement.getMandatoryRelease()
(2, 6, 0, 'alpha', 0)

These respectively indicate the first release supporting from __future__ import with_statement and the first release to support it without using from __future__.

Also, read this:

>>> import __future__
>>> help(__future__)
Mark Tolonen
  • 148,243
  • 22
  • 160
  • 229
17

You only need it in Python 2.5. Older versions (<= 2.4) don't support it and newer versions (>= 2.6) have it enabled by default.

So if you want to support Python >= 2.5, you can simply put the from __future__ import with_statement at the beginning. For newer versions, it will simply be ignored.

AndiDog
  • 65,893
  • 20
  • 156
  • 201
2

From the doc:

New in version 2.5.
Lie Ryan
  • 58,581
  • 13
  • 95
  • 141