Python 2.7 有什麼新功能¶
- 作者:
A.M. Kuchling (amk at amk.ca)
This article explains the new features in Python 2.7. Python 2.7 was released on July 3, 2010.
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.
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.
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 https://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 https://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.
Python 2.x 的未來¶
Python 2.7 is the last major release in the 2.x series, as the Python maintainers have shifted the focus of their new feature development efforts to the Python 3.x series. This means that while Python 2 continues to receive bug fixes, and to be updated to build correctly on new hardware and versions of supported operated systems, there will be no new full feature releases for the language or standard library.
However, while there is a large common subset between Python 2.7 and Python 3, and many of the changes involved in migrating to that common subset, or directly to Python 3, can be safely automated, some other changes (notably those associated with Unicode handling) may require careful consideration, and preferably robust automated regression test suites, to migrate effectively.
This means that Python 2.7 will remain in place for a long time, providing a stable and supported base platform for production systems that have not yet been ported to Python 3. The full expected lifecycle of the Python 2.7 series is detailed in PEP 373.
Some key consequences of the long-term significance of 2.7 are:
As noted above, the 2.7 release has a much longer period of maintenance when compared to earlier 2.x versions. Python 2.7 is currently expected to remain supported by the core development team (receiving security updates and other bug fixes) until at least 2020 (10 years after its initial release, compared to the more typical support period of 18--24 months).
As the Python 2.7 standard library ages, making effective use of the Python Package Index (either directly or via a redistributor) becomes more important for Python 2 users. In addition to a wide variety of third party packages for various tasks, the available packages include backports of new modules and features from the Python 3 standard library that are compatible with Python 2, as well as various tools and libraries that can make it easier to migrate to Python 3. The Python Packaging User Guide provides guidance on downloading and installing software from the Python Package Index.
While the preferred approach to enhancing Python 2 is now the publication of new packages on the Python Package Index, this approach doesn't necessarily work in all cases, especially those related to network security. In exceptional cases that cannot be handled adequately by publishing new or updated packages on PyPI, the Python Enhancement Proposal process may be used to make the case for adding new features directly to the Python 2 standard library. Any such additions, and the maintenance releases where they were added, will be noted in the New Features Added to Python 2.7 Maintenance Releases section below.
For projects wishing to migrate from Python 2 to Python 3, or for library and framework developers wishing to support users on both Python 2 and Python 3, there are a variety of tools and guides available to help decide on a suitable approach and manage some of the technical details involved. The recommended starting point is the 如何將 Python 2 的程式碼移植到 Python 3 HOWTO guide.
Changes to the Handling of Deprecation Warnings¶
For Python 2.7, a policy decision was made to silence warnings only of
interest to developers by default. 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 became Python 3.2. (Discussed
on stdlib-sig and carried out in bpo-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').
The unittest module also automatically reenables deprecation warnings
when running tests.
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
withstatement.A new version of the
iolibrary, 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.memoryview物件。A small subset of the
importlibmodule, described below.The
repr()of a floatxis shorter in many cases: it's now based on the shortest decimal string that's guaranteed to round back tox. As in previous versions of Python, it's guaranteed thatfloat(repr(x))recoversx.Float-to-string and string-to-float conversions are correctly rounded. The
round()function is also now correctly rounded.The
PyCapsuletype, used to provide a C API for extension modules.PyLong_AsLongAndOverflow()C API 函式。
Other new Python3-mode warnings include:
operator.isCallable()andoperator.sequenceIncludes(), which are not supported in 3.x, now trigger warnings.The
-3switch now automatically enables the-Qwarnswitch that causes warnings about using classic division with integers and long integers.
PEP 372: Adding an Ordered Dictionary to collections¶
Regular Python dictionaries iterate over key/value pairs in arbitrary order.
Over the years, a number of authors have written alternative implementations
that remember the order that the keys were originally inserted. Based on
the experiences from those implementations, 2.7 introduces a new
OrderedDict class in the collections module.
The OrderedDict API provides the same interface as regular
dictionaries but iterates over keys and values in a guaranteed order
depending on when a key was first inserted:
>>> from collections import OrderedDict
>>> d = OrderedDict([('first', 1),
... ('second', 2),
... ('third', 3)])
>>> d.items()
[('first', 1), ('second', 2), ('third', 3)]
If a new entry overwrites an existing entry, the original insertion position is left unchanged:
>>> d['second'] = 4
>>> d.items()
[('first', 1), ('second', 4), ('third', 3)]
Deleting an entry and reinserting it will move it to the end:
>>> del d['second']
>>> d['second'] = 5
>>> d.items()
[('first', 1), ('third', 3), ('second', 5)]
The popitem() method has an optional last
argument that defaults to True. If last is true, the most recently
added key is returned and removed; if it's false, the
oldest key is selected:
>>> od = OrderedDict([(x,0) for x in range(20)])
>>> od.popitem()
(19, 0)
>>> od.popitem()
(18, 0)
>>> od.popitem(last=False)
(0, 0)
>>> od.popitem(last=False)
(1, 0)
Comparing two ordered dictionaries checks both the keys and values, and requires that the insertion order was the same:
>>> od1 = OrderedDict([('first', 1),
... ('second'