I'd like to use with
statement in Python 2.5 in some production code. It was backported, should I expect any problems (e.g. with availability/compatibility on other machines/etc)?
Is this code
from __future__ import with_statement
compatible with Python 2.6?
with_statement wasn't back ported but implemented in Python 2.5. Adding new keywords or syntax can break existing applications. With Python the way they decided to handle this is allow people to opt-in to those features early so you can slowly transition your code over.
From http://python.org/doc/2.5.2/ref/future.html
You can actually inspect futures to get information on when first supported, when the import isn't needed anymore, etc.
I personally have been heavily using the with_statement in Python 2.5 for well over a year and have not had issues. I also transparently run that code with Python 2.6. There are some weird corner cases they have worked at cleaning up in the language, mostly related to cleanly and correctly compacting nested with statements.
Yes, that statement is a no-operation in Python 2.6, so you can freely use it to make
with
a keyword in your 2.5 code as well, without affecting your code's operation in 2.6. This is in fact the general design intention of "importing from the future" in Python!You can call this in Python 2.6 and 3.0/1 without problems (it's a no-op there).