What’s New In Python 3.0
Author:Guido van Rossum
Release:3.0.1
Date:February 14, 2009
This article explains the new features in Python 3.0, compared to 2.6.
Python 3.0, also known as “Python 3000” or “Py3K”, is the first ever
intentionally backwards incompatible Python release. There are more
changes than in a typical release, and more that are important for all
Python users. Nevertheless, after digesting the changes, you’ll find
that Python really hasn’t changed all that much – by and large, we’re
mostly fixing well-known annoyances and warts, and removing a lot of
old cruft.
This article doesn’t attempt to provide a complete specification of
all new features, but instead tries to give a convenient overview.
For full details, you should refer to the documentation for Python
3.0, and/or the many PEPs referenced in the text. If you want to
understand the complete implementation and design rationale for a
particular feature, PEPs usually have more details than the regular
documentation; but note that PEPs usually are not kept up-to-date once
a feature has been fully implemented.
Due to time constraints this document is not as complete as it should
have been. As always for a new release, the Misc/NEWS file in the
source distribution contains a wealth of detailed information about
every small thing that was changed.
Common Stumbling Blocks
This section lists those few changes that are most likely to trip you
up if you’re used to Python 2.5.
Print Is A Function
The print statement has been replaced with a
function, with keyword arguments to replace most of the special syntax
of the old print statement (PEP 3105). Examples:
You can also customize the separator between items, e.g.:
which produces:
There are <4294967296> possibilities!
Note:
The function doesn’t support the “softspace” feature of
the old print statement. For example, in Python 2.x,
would write "A\nB\n"; but in Python 3.0,
.
Initially, you’ll be finding yourself typing the old print x
a lot in interactive mode. Time to retrain your fingers to type
print(x) instead!
When using the 2to3 source-to-source conversion tool, all
print statements are automatically converted to
function calls, so this is mostly a non-issue for
larger projects.
Views And Iterators Instead Of Lists
Some well-known APIs no longer return lists:
methods , and
return “views” instead of lists. For example,
this no longer works: . Use instead (this works in Python 2.5 too and is just
as efficient).
Also, the dict.iterkeys(), dict.iteritems() and
dict.itervalues() methods are no longer supported.
and return iterators. If you really need
a list, a quick fix is e.g. list(map(...)), but a better fix is
often to use a list comprehension (especially when the original code
uses ), or rewriting the code so it doesn’t need a
list at all. Particularly tricky is invoked for the
side effects of the function; the correct transformation is to use a
regular loop (since creating a list would just be
wasteful).
now behaves like xrange() used to behave, except
it works with values of arbitrary size. The latter no longer
exists.
now returns an iterator.
Ordering Comparisons
Python 3.0 has simplified the rules for ordering comparisons:
The ordering comparison operators (<, <=, >=, >)
raise a TypeError exception when the operands don’t have a
meaningful natural ordering. Thus, expressions like , or are no longer valid, and e.g. raises instead of returning
False. A corollary is that sorting a heterogeneous list
no longer makes sense – all the elements must be comparable to each
other. Note that this does not apply to the == and !=
operators: objects of different incomparable types always compare
unequal to each other.
builtin.sorted() and list.sort() no longer accept the
cmp argument providing a comparison function. Use the key
argument instead. N.B. the key and reverse arguments are now
“keyword-only”.
The cmp() function should be treated as gone, and the __cmp__()
special method is no longer supported. Use for sorting,
with , and other rich comparisons as needed.
(If you really need the cmp() functionality, you could use the
expression as the equivalent for cmp(a, b).)
Integers
PEP 0237: Essentially, long renamed to .
That is, there is only one built-in integral type, named
; but it behaves mostly like the old long type.
PEP 0238: An expression like 1/2 returns a float. Use
1//2 to get the truncating behavior. (The latter syntax has
existed for years, at least since Python 2.2.)
The sys.maxint constant was removed, since there is no
longer a limit to the value of integers. However,
can be used as an integer larger than any practical list or string
index. It conforms to the implementation’s “natural” integer size
and is typically the same as sys.maxint in previous releases
on the same platform (assuming the same build options).
The of a long integer doesn’t include the trailing L
anymore, so code that unconditionally strips that character will
chop off the last digit instead. (Use instead.)
Octal literals are no longer of the form 0720; use 0o720
instead.
(责任编辑:JavaVideo) |