with语句-Python 2.5的backport [英] with statement - backport for Python 2.5

查看:118
本文介绍了with语句-Python 2.5的backport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些生产代码中使用Python 2.5中的with语句.它被反向移植,我是否应该预料到任何问题(例如,与其他计算机/等等上的可用性/兼容性)?

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

这是代码

from __future__ import with_statement

与Python 2.6兼容吗?

compatible with Python 2.6?

推荐答案

with_statement并未回移植,但已在Python 2.5中实现.添加新的关键字或语法可能会破坏现有的应用程序.使用Python,他们决定处理此问题的方式是允许人们尽早选择加入这些功能,以便您可以缓慢地过渡代码.

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.

来自 http://python.org/doc/2.5.2/ref/future.html

未来的陈述是对 那个特定模块的编译器 应该使用语法或 语义将在 指定将来的Python版本. 将来的声明旨在 简化向的未来版本的迁移 引入不兼容的Python 更改语言.它允许使用 每个模块上的新功能 发布之前的基础 功能成为标准.

A future statement is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard.

您实际上可以检查期货,以获取有关何时首次受支持,何时不再需要导入等的信息.

You can actually inspect futures to get information on when first supported, when the import isn't needed anymore, etc.

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import __future__
>>> dir(__future__)
['CO_FUTURE_ABSOLUTE_IMPORT', 'CO_FUTURE_DIVISION', 'CO_FUTURE_WITH_STATEMENT', 'CO_GENERATOR_ALLOWED', 'CO_NESTED', '_Feature', '__all__', '__builtins__',
__doc__', '__file__', '__name__', 'absolute_import', 'all_feature_names', 'division', 'generators', 'nested_scopes', 'with_statement']
>>> __future__.with_statement
_Feature((2, 5, 0, 'alpha', 1), (2, 6, 0, 'alpha', 0), 32768)
>>>

我个人一年多以来一直在大量使用Python 2.5中的with_statement,并且没有遇到任何问题.我还使用Python 2.6透明地运行该代码.他们用这种语言清理过一些奇怪的极端情况,主要与嵌套在语句上的干净,正确压缩有关.

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.

这篇关于with语句-Python 2.5的backport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆