Python 2.7的新特性

What’s New in Python 2.7

Author: A.M. Kuchling (amk at amk.ca)
Release: 2.7.2
Date: August 25, 2011

This article explains the new features in Python 2.7. Python 2.7 was released on July 3, 2010.

本文解释了Python2.7中的新特性。该版本于2010年7月3日发布。

Numeric handling has been improved in many ways, for both floating-point numbers and for the Decimal class. There are some useful additions to the standard library, such as a greatly enhanced unittest module, the argparse module for parsing command-line options, convenient OrderedDict and Counter classes in the collections module, and many other improvements.

数值处理能力在许多方面有所提高, 包括浮点数和小数处理。一些实用的模块加入到了标准库中,例如:

-- 大幅加强了的 unittest 模块;

-- 用于解析命令行参数的 argparse 模块;

-- collections 模块中便利的 OrderedDict 和 Counter 类;

-- 以及许多其他的改进

Python 2.7 is planned to be the last of the 2.x releases, so we worked on making it a good release for the long term. To help with porting to Python 3, several new features from the Python 3.x series have been included in 2.7.

计划中 Python 2.7 会是 2.x 家族的最后一员, 所以长期以来我们致力于将其打造成为良好的发布版本。 为了方便大家逐渐适应 Python 3, 一些 Python 3.x 系列的特性被引入了 Python 2.7。

This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.7 at http://docs.python.org. If you want to understand the rationale for the design and implementation, refer to the PEP for a particular new feature or the issue on http://bugs.python.org in which a change was discussed. Whenever possible, “What’s New in Python” links to the bug/patch item for each change.

本文不打算提供关于新特性的完整描述,而是提供了一个方便的概述。对于完全的细节,你应该去参考http://bugs.python.org 上的Python2.7文档。如果你想要理解设计和实现的基本原理,参考关于某一特性新属性的PEP或在http://bugs.python.org 上的对于某一问题改动的讨论。只要可能,本文链接了对于每个改动的漏洞/补丁。

The Future for Python 2.x

Python 2.7 is intended to be the last major release in the 2.x series. The Python maintainers are planning to focus their future efforts on the Python 3.x series.

This means that 2.7 will remain in place for a long time, running production systems that have not been ported to Python 3.x. Two consequences of the long-term significance of 2.7 are:

  • It’s very likely the 2.7 release will have a longer period of maintenance compared to earlier 2.x versions. Python 2.7 will continue to be maintained while the transition to 3.x continues, and the developers are planning to support Python 2.7 with bug-fix releases beyond the typical two years.

  • 对比与早期的2.X版本,2.7发布版本非常可能会拥有更长的维护时间。在过渡到3.X的同时,Python 2.7将会继续维护,并且开发者计划支持2.7的漏洞修复达两年以上。
  • A policy decision was made to silence warnings only of interest to developers. DeprecationWarning and its descendants are now ignored unless otherwise requested, preventing users from seeing warnings triggered by an application. This change was also made in the branch that will become Python 3.2. (Discussed on stdlib-sig and carried out in issue 7319.)

    In previous releases, DeprecationWarning messages were enabled by default, providing Python developers with a clear indication of where their code may break in a future major version of Python.

    However, there are increasingly many users of Python-based applications who are not directly involved in the development of those applications. DeprecationWarning messages are irrelevant to such users, making them worry about an application that’s actually working correctly and burdening application developers with responding to these concerns.

    You can re-enable display of DeprecationWarning messages by running Python with the -Wdefault (short form: -Wd) switch, or by setting the PYTHONWARNINGS environment variable to "default" (or "d") before running Python. Python code can also re-enable them by calling warnings.simplefilter('default').

  • 因为开发者的意愿,做出了关闭警告的决定。DeprecationWarning和它的后代现在是被忽略的,除非有其他的请求,否则将会阻止用户看到由应用触发的警告。

Python 3.1 Features

Much as Python 2.6 incorporated features from Python 3.0, version 2.7 incorporates some of the new features in Python 3.1. The 2.x series continues to provide tools for migrating to the 3.x series.

A partial list of 3.1 features that were backported to 2.7:

  • The syntax for set literals ({1,2,3} is a mutable set).
  • Dictionary and set comprehensions ({i: i*2 for i in range(3)}).
  • Multiple context managers in a single with statement.
  • A new version of the io library, rewritten in C for performance.
  • The ordered-dictionary type described in PEP 372: Adding an Ordered Dictionary to collections.
  • The new "," format specifier described in PEP 378: Format Specifier for Thousands Separator.
  • The memoryview object.
  • A small subset of the importlib module, described below.
  • The repr() of a float x is shorter in many cases: it’s now based on the shortest decimal string that’s guaranteed to round back to x. As in previous versions of Python, it’s guaranteed that float(repr(x)) recovers x.
  • Float-to-string and string-to-float conversions are correctly rounded. The round() function is also now correctly rounded.
  • The PyCapsule type, used to provide a C API for extension modules.
  • The PyLong_AsLongAndOverflow() C API function.

Other new Python3-mode warnings include:

posted @ 2011-08-25 21:26  牛皮糖NewPtone  阅读(2322)  评论(0编辑  收藏  举报