Python 3.0 有什么新变化
***********************

作者:
   Guido van Rossum

本文介绍 Python 3.0 与 2.6 相比的新特性。 Python 3.0 也被称为 "Python
3000" 或 "Py3K"，是有史以来第一个 *有意向下不兼容* 的 Python 版本。
Python 3.0 于 2008 年 12 月 3 日发布。 与一般的发布版本相比，Python
3.0 有更多的变化，而且对所有 Python 用户都很重要。 不过，在理解了这些
改动之后，您会发现 Python 其实并没有太大的变化 -- 总的来说，我们主要是
修复了一些众所周知的问题和缺陷，并删除了许多旧的垃圾。

本文并不试图提供所有新特性的完整规范，而是试图提供一个方便的概述。 要
了解完整的细节，您应该参考 Python 3.0 的文档和/或文中引用的许多 PEP。
如果您想了解某个特性的完整实现和设计原理，PEP 通常比常规文档有更多的细
节；但要注意的是，一旦某个特性被完全实现，PEP 通常不会保持更新。

由于时间有限，本文档不够完整。 对于新发布的版本，源代码发行版中的
"Misc/NEWS" 文件总是包含大量关于每一个细小改动的详细信息。


常见的绊脚石
============

本节列出了在你已习惯了 Python 2.5 的情况下最有可能让您感到困惑的几处更
改。


Print 是函数
------------

"print" 语句已被 "print()" 函数取代，其关键字参数取代了旧 "print" 语句
(**PEP 3105**) 的大部分特殊语法。 示例:

   Old: print "The answer is", 2*2
   New: print("The answer is", 2*2)

   Old: print x,           # Trailing comma suppresses newline
   New: print(x, end=" ")  # Appends a space instead of a newline

   Old: print              # Prints a newline
   New: print()            # You must call the function!

   Old: print >>sys.stderr, "fatal error"
   New: print("fatal error", file=sys.stderr)

   Old: print (x, y)       # prints repr((x, y))
   New: print((x, y))      # Not the same as print(x, y)!

你还可以自定义条目间的分隔符，例如

   print("There are <", 2**32, "> possibilities!", sep="")

这将产生如下结果:

   There are <4294967296> possibilities!

注意

* "print()" 函数不支持旧 "print" 语句的 "softspace" 功能。例如，在
  Python 2.x 中，"print "A\n", "B"" 会写入 ""A\nB\n""；但在 Python 3.0
  中，"print("A\n", "B")" 会写入 ""A\n B\n""。

* 最初，您会发现自己在交互模式下经常输入旧的 "print x" 。是时候重新训
  练你的手指以输入 "print(x)" 了！

* 使用 "2to3" 源代码到源代码转换工具时，所有 "print" 语句都会自动转换
  为 "print()" 函数调用，因此对于大型项目来说，这基本上不是问题。


用视图和迭代器取代列表
----------------------

某些知名的 API 将不再返回列表:

* "dict" 方法 "dict.keys()"、"dict.items()" 和 "dict.values()" 返回 “
  视图” 而不是列表。 例如，这个写法不再有效: "k = d.keys(); k.sort()"
  。 请使用 "k = sorted(d)" 代替（这在 Python 2.5 中也有效，而且同样高
  效）。

* 此外，不再支持 "dict.iterkeys()"、"dict.iteritems()" 和
  "dict.itervalues()" 方法。

* "map()" 和 "filter()" 均返回迭代器。 如果你确实需要一个列表并且所有
  输入序列的长度相等，简单的解决办法是将 "map()" 包装在 "list()" 中，
  例如 "list(map(...))"，但更好的办法通常是使用列表推导式（特别是当原
  始代码使用了 "lambda" 的时候），或是重写代码使得它完全不需要列表。
  还有一种特殊技巧是将 "map()" 作为函数的附带影响被唤起；正确的转换方
  式是使用一个常规的 "for" 循环（因为创建列表会浪费资源）。

  如果输入序列的长度不相等，"map()" 将在最短序列的终点停止。 为了与
  Python 2.x 中的 "map()" 完全兼容，也可将序列包装在
  "itertools.zip_longest()" 中，例如将 "map(func, *sequences)" 变成
  "list(map(func, itertools.zip_longest(*sequences)))"。

* 现在 "range()" 的行为与过去 "xrange()" 的行为类似，区别在于它可以处
  理任意大小的值。   后者已不复存在。

* "zip()" 现在将返回一个迭代器。


排序比较
--------

Python 3.0 简化了排序比较的规则：

* 当操作数不存在有意义的自然排序时，排序比较操作符 ("<", "<=", ">=",
  ">") 会引发 TypeError 异常。 因此，像 "1 < ''", "0 > None" 或 "len
  <= len" 这样的表达式不再有效，例如 "None < None" 会引发 "TypeError"
  而不是返回 "False"。 由此推论，对异构列表进行排序不再有意义 —— 所有
  元素必须相互可比。 请注意，这不适用于 "==" 和 "!=" 操作符：不同的不
  可比类型的对象总是互不相等的。

* "builtin.sorted()" 和 "list.sort()" 不再接受提供比较函数的 *cmp* 参
  数。 请使用 *key* 参数。 注意 *key* 和 *reverse* 参数现在是“仅限关键
  字”参数。

* "cmp()" 函数应视为已不复存在，而 "__cmp__()" 特殊方法也不再支持。 请
  使用 "__lt__()" 进行排序，使用 "__eq__()" 与 "__hash__()" 进行比较，
  并根据需要使用其他的丰富比较方法。 （如果确实需要 "cmp()" 功能，可以
  使用表达式 "(a > b) - (a < b)" 以实现 "cmp(a, b)"。）


整数
----

* **PEP 237**: 在实质上，"long" 已被重命名为 "int"。 也就是说，现在只
  有一种内置整数类型，叫做 "int"；但其行为更像是旧的 "long" 类型。

* **PEP 238**: 像 "1/2" 这样的表达式将返回一个浮点数。 请使用 "1//2"
  来得到取整的行为。 （后面这种语法已存在多年，至少从 Python 2.2 起就
  有了。）

* "sys.maxint" 常量已被删除，因为整数值不再有限制。 不过，
  "sys.maxsize" 仍可用作大于任何实际列表或字符串索引的整数。 它符合实
  现的“自然”整数大小，通常与同一平台上以前版本中的 "sys.maxint" 相同（
  假设使用相同的构建选项）。

* 长整数的 "repr()" 不再包括尾部的 "L"，因此无条件地删除该字符的代码会
  删除最后一位数字。 （请使用 "str()" 代替。）

* 八进制数字面值不再是 "0720" 的形式；而是改用 "0o720" 的形式。


文本与数据而不是 Unicode 与 8 比特位
------------------------------------

你对二进制数据和 Unicode 的所有认知都已改变。

* Python 3.0 uses the concepts of *text* and (binary) *data* instead
  of Unicode strings and 8-bit strings.  All text is Unicode; however
  *encoded* Unicode is represented as binary data.  The type used to
  hold text is "str", the type used to hold data is "bytes".  The
  biggest difference with the 2.x situation is that any attempt to mix
  text and data in Python 3.0 raises "TypeError", whereas if you were
  to mix Unicode and 8-bit strings in Python 2.x, it would work if the
  8-bit string happened to contain only 7-bit (ASCII) bytes, but you
  would get "UnicodeDecodeError" if it contained non-ASCII values.
  This value-specific behavior has caused numerous sad faces over the
  years.

* As a consequence of this change in philosophy, pretty much all code
  that uses Unicode, encodings or binary data most likely has to
  change.  The change is for the better, as in the 2.x world there
  were numerous bugs having to do with mixing encoded and unencoded
  text.  To be prepared in Python 2.x, start using "unicode" for all
  unencoded text, and "str" for binary or encoded data only.  Then the
  "2to3" tool will do most of the work for you.

* 你不能再使用 "u"..."" 字面值来表示 Unicode 文本。 不过，你必须使用
  "b"..."" 字面值来表示二进制数据。

* As the "str" and "bytes" types cannot be mixed, you must always
  explicitly convert between them.  Use "str.encode()" to go from
  "str" to "bytes", and "bytes.decode()" to go from "bytes" to "str".
  You can also use "bytes(s, encoding=...)" and "str(b,
  encoding=...)", respectively.

* 与 "str" 一样，"bytes" 类型是不可变的。 还有一个单独的 *mutable* 类
  型用于保存缓冲二进制数据，即 "bytearray"。 几乎所有接受 "bytes" 的应
  用程序接口也接受 "bytearray"。 可变 API 基于
  "collections.MutableSequence"。

* 原始字符串字面中的所有反斜线均按字面解释。 这意味着原始字符串中的
  "'\U'" 和 "'\u'" 转义符不会被特殊处理。 例如，在 Python 3.0 中，
  "r'\u20ac'" 是一个包含 6 个字符的字符串，而在 2.6 中，"ur'\u20ac'"
  是一个 “欧元” 字符。 （当然，这种变化只影响原始字符串的字面意义；在
  Python 3.0 中，欧元字符是 "'\u20ac'"。）

* 内置的 "basestring" 抽象类型已被移除，请使用 "str" 代替。 "str" 和
  "bytes" 类型在功能上没有足够的共通性，因此不需要共享基类。 "2to3" 工
  具（见下文）用 "str" 替换了每一个 "basestring"。

* Files opened as text files (still the default mode for "open()")
  always use an encoding to map between strings (in memory) and bytes
  (on disk).  Binary files (opened with a "b" in the mode argument)
  always use bytes in memory.  This means that if a file is opened
  using an incorrect mode or encoding, I/O will likely fail loudly,
  instead of silently producing incorrect data.  It also means that
  even Unix users will have to specify the correct mode (text or
  binary) when opening a file.  There is a platform-dependent default
  encoding, which on Unixy platforms can be set with the "LANG"
  environment variable (and sometimes also with some other platform-
  specific locale-related environment variables).  In many cases, but
  not all, the system default is UTF-8; you should never count on this
  default.  Any application reading or writing more than pure ASCII
  text should probably have a way to override the encoding. There is
  no longer any need for using the encoding-aware streams in the
  "codecs" module.

* "sys.stdin"、"sys.stdout" 和 "sys.stderr" 的初始值现在是仅 Unicode
  的文本文件（即它们是 "io.TextIOBase" 的实例）。 要使用这些数据流读写
  字节数据，需要使用它们的 "io.TextIOBase.buffer" 属性。

* Filenames are passed to and returned from APIs as (Unicode) strings.
  This can present platform-specific problems because on some
  platforms filenames are arbitrary byte strings.  (On the other hand,
  on Windows filenames are natively stored as Unicode.)  As a work-
  around, most APIs (e.g. "open()" and many functions in the "os"
  module) that take filenames accept "bytes" objects as well as
  strings, and a few APIs have a way to ask for a "bytes" return
  value.  Thus, "os.listdir()" returns a list of "bytes" instances if
  the argument is a "bytes" instance, and "os.getcwdb()" returns the
  current working directory as a "bytes" instance.  Note that when
  "os.listdir()" returns a list of strings, filenames that cannot be
  decoded properly are omitted rather than raising "UnicodeError".

* 当系统提供的字节无法使用默认编码进行解释时，一些系统 API，如
  "os.environ" 和 "sys.argv"，也会出现问题。 最好的办法可能是设置
  "LANG" 变量并重新运行程序。

* **PEP 3138**: 字符串的 "repr()" 将不再转义非 ASCII 字符。 不过，它仍
  然会转义控制字符和在 Unicode 标准中具有不可打印状态的码位。

* **PEP 3120**：现在默认的源码编码格式是UTF-8。

* **PEP 3131**: 现在允许在标识符中使用非 ASCII 字符（不过，标准库中的
  异常和注释中的贡献者名字仍然只使用 ASCII 字符）。

* The "StringIO" and "cStringIO" modules are gone.  Instead, import
  the "io" module and use "io.StringIO" or "io.BytesIO" for text and
  data respectively.

* 另请参阅 Unicode 指南，其内容已针对 Python 3.0 进行更新。


语法变化概述
============

本节提供了 Python 3.0 中每个 *语法* 变化的简要说明。


新语法
------

* **PEP 3107**: Function argument and return value annotations.  This
  provides a standardized way of annotating a function's parameters
  and return value.  There are no semantics attached to such
  annotations except that they can be introspected at runtime using
  the "__annotations__" attribute.  The intent is to encourage
  experimentation through metaclasses, decorators or frameworks.

* **PEP 3102**：仅限关键字参数。在参数列表``*args`` 之后出现的命名参数
  *必须* 在调用中使用关键字语法指定。也可以在参数列表中使用``*``来表示
  不接受长度可变的参数列表，但可以使用只包含关键字的参数。

* 类定义中的基类列表后允许使用关键字参数。  这是用于指定元类的新约定（
  见下一节），但也可用于其他目的，只要元类支持它。

* **PEP 3104**: "nonlocal" statement.  Using "nonlocal x" you can now
  assign directly to a variable in an outer (but non-global) scope.
  "nonlocal" is a new reserved word.

* **PEP 3132**: Extended Iterable Unpacking.  You can now write things
  like "a, b, *rest = some_sequence".  And even "*rest, a = stuff".
  The "rest" object is always a (possibly empty) list; the right-hand
  side may be any iterable.  Example:

     (a, *rest, b) = range(5)

  This sets *a* to "0", *b* to "4", and *rest* to "[1, 2, 3]".

* Dictionary comprehensions: "{k: v for k, v in stuff}" means the same
  thing as "dict(stuff)" but is more flexible.  (This is **PEP 274**
  vindicated. :-)

* Set literals, e.g. "{1, 2}".  Note that "{}" is an empty dictionary;
  use "set()" for an empty set.  Set comprehensions are also
  supported; e.g., "{x for x in stuff}" means the same thing as
  "set(stuff)" but is more flexible.

* New octal literals, e.g. "0o720" (already in 2.6).  The old octal
  literals ("0720") are gone.

* New binary literals, e.g. "0b1010" (already in 2.6), and there is a
  new corresponding built-in function, "bin()".

* Bytes literals are introduced with a leading "b" or "B", and there
  is a new corresponding built-in function, "bytes()".


语法变化
--------

* **PEP 3109** 和 **PEP 3134**: 新增 "raise" 语句的语法: "raise
  [*expr* [from *expr*]]"。 见下文。

* 现在 "as" 和 "with" 是保留关键字。 （实际是从 2.6 开始。）

* "True", "False" 和 "None" 已成为保留关键字。 （2.6 已经对 "None" 部
  分强制应用限制。）

* 将 "except" *exc*, *var* 改为 "except" *exc* "as" *var*。 参见 **PEP
  3110**。

* **PEP 3115**: 新的元类语法。 替换:

     class C:
         __metaclass__ = M
         ...

  你现在需要使用:

     class C(metaclass=M):
         ...

  不再支持全局模块变量 "__metaclass__"。  (它是一个“拐杖”，可以使默认
  使用新风格类变得更容易，而无需从 "object" 派生每个类。）

* List comprehensions no longer support the syntactic form "[... for
  *var* in *item1*, *item2*, ...]".  Use "[... for *var* in (*item1*,
  *item2*, ...)]" instead. Also note that list comprehensions have
  different semantics: they are closer to syntactic sugar for a
  generator expression inside a "list()" constructor, and in
  particular the loop control variables are no longer leaked into the
  surrounding scope.

* *ellipsis* ("...") 可以在任何地方作为原子表达式使用。（以前只允许在
  片段中使用。）另外，现在 *必须* 拼写为``...`` 。（以前也可以拼写为
  ``. . .`` ，这只是一个偶然的语法。）


移除的语法
----------

* **PEP 3113**: Tuple parameter unpacking removed.  You can no longer
  write "def foo(a, (b, c)): ...". Use "def foo(a, b_c): b, c = b_c"
  instead.

* Removed backticks (use "repr()" instead).

* Removed "<>" (use "!=" instead).

* Removed keyword: "exec()" is no longer a keyword; it remains as a
  function.  (Fortunately the function syntax was also accepted in
  2.x.)  Also note that "exec()" no longer takes a stream argument;
  instead of "exec(f)" you can use "exec(f.read())".

* Integer literals no longer support a trailing "l" or "L".

* String literals no longer support a leading "u" or "U".

* The "from" *module* "import" "*" syntax is only allowed at the
  module level, no longer inside functions.

* The only acceptable syntax for relative imports is "from .[*module*]
  import *name*".  All "import" forms not starting with "." are
  interpreted as absolute imports.  (**PEP 328**)

* Classic classes are gone.


Changes Already Present In Python 2.6
=====================================

Since many users presumably make the jump straight from Python 2.5 to
Python 3.0, this section reminds the reader of new features that were
originally designed for Python 3.0 but that were back-ported to Python
2.6.  The corresponding sections in Python 2.6 有什么新变化 should be
consulted for longer descriptions.

* PEP 343: "with" 语句.  The "with" statement is now a standard
  feature and no longer needs to be imported from the "__future__".
  Also check out 编写上下文管理器 and contextlib 模块.

* PEP 366: 从主模块显式相对导入.  This enhances the usefulness of the
  "-m" option when the referenced module lives in a package.

* PEP 370: 分用户的 site-packages 目录.

* PEP 371: 多任务处理包.

* PEP 3101: 高级字符串格式.  Note: the 2.6 description mentions the
  "format()" method for both 8-bit and Unicode strings.  In 3.0, only
  the "str" type (text strings with Unicode support) supports this
  method; the "bytes" type does not.  The plan is to eventually make
  this the only API for string formatting, and to start deprecating
  the "%" operator in Python 3.1.

* PEP 3105: print 改为函数.  This is now a standard feature and no
  longer needs to be imported from "__future__".  More details were
  given above.

* PEP 3110: 异常处理的变更.  The "except" *exc* "as" *var* syntax is
  now standard and "except" *exc*, *var* is no longer supported.  (Of
  course, the "as" *var* part is still optional.)

* PEP 3112: 字节字面值.  The "b"..."" string literal notation (and its
  variants like "b'...'", "b"""..."""", and "br"..."") now produces a
  literal of type "bytes".

* PEP 3116: 新 I/O 库: "io" 模块现在是进行文件输入/输出的标准方法。 内
  置的 "open()" 函数现在是 "io.open()" 的别名，并增加了 *encoding*、
  *errors*、*newline* 和 *closefd* 等关键字参数。 还要注意的是，无效的
  *mode* 参数现在会引发 "ValueError"，而不是 "IOError"。 文本文件对象
  底层的二进制文件对象可以像 "f.buffer" 一样访问（但要注意，文本对象会
  为自己保留一个缓冲区，以加快编码和解码操作。）

* PEP 3118: 修改缓冲区协议.  The old builtin "buffer()" is now really
  gone; the new builtin "memoryview()" provides (mostly) similar
  functionality.

* PEP 3119: 抽象基类.  The "abc" module and the ABCs defined in the
  "collections" module plays a somewhat more prominent role in the
  language now, and built-in collection types like "dict" and "list"
  conform to the "collections.MutableMapping" and
  "collections.MutableSequence" ABCs, respectively.

* PEP 3127: 整型文字支持和语法.  As mentioned above, the new octal
  literal notation is the only one supported, and binary literals have
  been added.

* PEP 3129: 类装饰器.

* PEP 3141: 数字的类型层级结构.  The "numbers" module is another new
  use of ABCs, defining Python's "numeric tower".  Also note the new
  "fractions" module which implements "numbers.Rational".


库的修改
========

Due to time constraints, this document does not exhaustively cover the
very extensive changes to the standard library.  **PEP 3108** is the
reference for the major changes to the library.  Here's a capsule
review:

* Many old modules were removed.  Some, like "gopherlib" (no longer
  used) and "md5" (replaced by "hashlib"), were already deprecated by
  **PEP 4**.  Others were removed as a result of the removal of
  support for various platforms such as Irix, BeOS and Mac OS 9 (see
  **PEP 11**).  Some modules were also selected for removal in Python
  3.0 due to lack of use or because a better replacement exists.  See
  **PEP 3108** for an exhaustive list.

* The "bsddb3" package was removed because its presence in the core
  standard library has proved over time to be a particular burden for
  the core developers due to testing instability and Berkeley DB's
  release schedule.  However, the package is alive and well,
  externally maintained at
  https://www.jcea.es/programacion/pybsddb.htm.

* Some modules were renamed because their old name disobeyed **PEP
  8**, or for various other reasons.  Here's the list:

  +-------------------------+-------------------------+
  | 旧名称                  | 新名称                  |
  |=========================|=========================|
  | _winreg                 | winreg                  |
  +-------------------------+-------------------------+
  | ConfigParser            | configparser            |
  +-------------------------+-------------------------+
  | copy_reg                | copyreg                 |
  +-------------------------+-------------------------+
  | Queue                   | queue                   |
  +-------------------------+-------------------------+
  | SocketServer            | socketserver            |
  +-------------------------+-------------------------+
  | markupbase              | _markupbase             |
  +-------------------------+-------------------------+
  | repr                    | reprlib                 |
  +-------------------------+-------------------------+
  | test.test_support       | test.support            |
  +-------------------------+-------------------------+

* A common pattern in Python 2.x is to have one version of a module
  implemented in pure Python, with an optional accelerated version
  implemented as a C extension; for example, "pickle" and "cPickle".
  This places the burden of importing the accelerated version and
  falling back on the pure Python version on each user of these
  modules.  In Python 3.0, the accelerated versions are considered
  implementation details of the pure Python versions. Users should
  always import the standard version, which attempts to import the
  accelerated version and falls back to the pure Python version.  The
  "pickle" / "cPickle" pair received this treatment.  The "profile"
  module is on the list for 3.1.  The "StringIO" module has been
  turned into a class in the "io" module.

* Some related modules have been grouped into packages, and usually
  the submodule names have been simplified.  The resulting new
  packages are:

  * "dbm" ("anydbm", "dbhash", "dbm", "dumbdbm", "gdbm", "whichdb").

  * "html" ("HTMLParser", "htmlentitydefs").

  * "http" ("httplib", "BaseHTTPServer", "CGIHTTPServer",
    "SimpleHTTPServer", "Cookie", "cookielib").

  * "tkinter" (all "Tkinter"-related modules except "turtle").  The
    target audience of "turtle" doesn't really care about "tkinter".
    Also note that as of Python 2.6, the functionality of "turtle" has
    been greatly enhanced.

  * "urllib" ("urllib", "urllib2", "urlparse", "robotparse").

  * "xmlrpc" ("xmlrpclib", "DocXMLRPCServer", "SimpleXMLRPCServer").

Some other changes to standard library modules, not covered by **PEP
3108**:

* Killed "sets".  Use the built-in "set()" class.

* Cleanup of the "sys" module: removed "sys.exitfunc()",
  "sys.exc_clear()", "sys.exc_type", "sys.exc_value",
  "sys.exc_traceback".  (Note that "sys.last_type" etc. remain.)

* Cleanup of the "array.array" type: the "read()" and "write()"
  methods are gone; use "fromfile()" and "tofile()" instead.  Also,
  the "'c'" typecode for array is gone -- use either "'b'" for bytes
  or "'u'" for Unicode characters.

* Cleanup of the "operator" module: removed "sequenceIncludes()" and
  "isCallable()".

* Cleanup of the "thread" module: "acquire_lock()" and
  "release_lock()" are gone; use "acquire()" and "release()" instead.

* Cleanup of the "random" module: removed the "jumpahead()" API.

* The "new" module is gone.

* The functions "os.tmpnam()", "os.tempnam()" and "os.tmpfile()" have
  been removed in favor of the "tempfile" module.

* The "tokenize" module has been changed to work with bytes.  The main
  entry point is now "tokenize.tokenize()", instead of
  generate_tokens.

* "string.letters" and its friends ("string.lowercase" and
  "string.uppercase") are gone.  Use "string.ascii_letters" etc.
  instead.  (The reason for the removal is that "string.letters" and
  friends had locale-specific behavior, which is a bad idea for such
  attractively named global "constants".)

* Renamed module "__builtin__" to "builtins" (removing the
  underscores, adding an 's').  The "__builtins__" variable found in
  most global namespaces is unchanged.  To modify a builtin, you
  should use "builtins", not "__builtins__"!


**PEP 3101**: A New Approach To String Formatting
=================================================

* A new system for  built-in string formatting operations replaces the
  "%" string  formatting operator.   (However, the "%"  operator is
  still supported;  it will  be deprecated in  Python 3.1  and removed
  from the language at some later time.)  Read **PEP 3101** for the
  full scoop.


Changes To Exceptions
=====================

The APIs for raising and catching exception have been cleaned up and
new powerful features added:

* **PEP 352**: All exceptions must be derived (directly or indirectly)
  from "BaseException".  This is the root of the exception hierarchy.
  This is not new as a recommendation, but the *requirement* to
  inherit from "BaseException" is new.  (Python 2.6 still allowed
  classic classes to be raised, and placed no restriction on what you
  can catch.)  As a consequence, string exceptions are finally truly
  and utterly dead.

* Almost all exceptions should actually derive from "Exception";
  "BaseException" should only be used as a base class for exceptions
  that should only be handled at the top level, such as "SystemExit"
  or "KeyboardInterrupt".  The recommended idiom for handling all
  exceptions except for this latter category is to use "except"
  "Exception".

* "StandardError" was removed.

* Exceptions no longer behave as sequences.  Use the "args" attribute
  instead.

* **PEP 3109**: Raising exceptions.  You must now use "raise
  *Exception*(*args*)" instead of "raise *Exception*, *args*".
  Additionally, you can no longer explicitly specify a traceback;
  instead, if you *have* to do this, you can assign directly to the
  "__traceback__" attribute (see below).

* **PEP 3110**: Catching exceptions.  You must now use "except
  *SomeException* as *variable*" instead of "except *SomeException*,
  *variable*".  Moreover, the *variable* is explicitly deleted when
  the "except" block is left.

* **PEP 3134**: Exception chaining.  There are two cases: implicit
  chaining and explicit chaining.  Implicit chaining happens when an
  exception is raised in an "except" or "finally" handler block.  This
  usually happens due to a bug in the handler block; we call this a
  *secondary* exception.  In this case, the original exception (that
  was being handled) is saved as the "__context__" attribute of the
  secondary exception. Explicit chaining is invoked with this syntax:

     raise SecondaryException() from primary_exception

  (where *primary_exception* is any expression that produces an
  exception object, probably an exception that was previously caught).
  In this case, the primary exception is stored on the "__cause__"
  attribute of the secondary exception.  The traceback printed when an
  unhandled exception occurs walks the chain of "__cause__" and
  "__context__" attributes and prints a separate traceback for each
  component of the chain, with the primary exception at the top.
  (Java users may recognize this behavior.)

* **PEP 3134**: Exception objects now store their traceback as the
  "__traceback__" attribute.  This means that an exception object now
  contains all the information pertaining to an exception, and there
  are fewer reasons to use "sys.exc_info()" (though the latter is not
  removed).

* A few exception messages are improved when Windows fails to load an
  extension module.  For example, "error code 193" is now "%1 is not a
  valid Win32 application".  Strings now deal with non-English
  locales.


Miscellaneous Other Changes
===========================


Operators And Special Methods
-----------------------------

* "!=" now returns the opposite of "==", unless "==" returns
  "NotImplemented".

* The concept of "unbound methods" has been removed from the language.
  When referencing a method as a class attribute, you now get a plain
  function object.

* "__getslice__()", "__setslice__()" and "__delslice__()" were killed.
  The syntax "a[i:j]" now translates to "a.__getitem__(slice(i, j))"
  (or "__setitem__()" or "__delitem__()", when used as an assignment
  or deletion target, respectively).

* **PEP 3114**: the standard "next()" method has been renamed to
  "__next__()".

* The "__oct__()" and "__hex__()" special methods are removed --
  "oct()" and "hex()" use "__index__()" now to convert the argument to
  an integer.

* Removed support for "__members__" and "__methods__".

* The function attributes named "func_X" have been renamed to use the
  "__X__" form, freeing up these names in the function attribute
  namespace for user-defined attributes.  To wit, "func_closure",
  "func_code", "func_defaults", "func_dict", "func_doc",
  "func_globals", "func_name" were renamed to "__closure__",
  "__code__", "__defaults__", "__dict__", "__doc__", "__globals__",
  "__name__", respectively.

* "__nonzero__()" is now "__bool__()".


Builtins
--------

* **PEP 3135**: New "super()".  You can now invoke "super()" without
  arguments and (assuming this is in a regular instance method defined
  inside a "class" statement) the right class and instance will
  automatically be chosen.  With arguments, the behavior of "super()"
  is unchanged.

* **PEP 3111**: "raw_input()" was renamed to "input()".  That is, the
  new "input()" function reads a line from "sys.stdin" and returns it
  with the trailing newline stripped. It raises "EOFError" if the
  input is terminated prematurely. To get the old behavior of
  "input()", use "eval(input())".

* A new built-in function "next()" was added to call the "__next__()"
  method on an object.

* The "round()" function rounding strategy and return type have
  changed.  Exact halfway cases are now rounded to the nearest even
  result instead of away from zero.  (For example, "round(2.5)" now
  returns "2" rather than "3".)  "round(x[, n])" now delegates to
  "x.__round__([n])" instead of always returning a float.  It
  generally returns an integer when called with a single argument and
  a value of the same type as "x" when called with two arguments.

* Moved "intern()" to "sys.intern()".

* Removed: "apply()".  Instead of "apply(f, args)" use "f(*args)".

* Removed "callable()".  Instead of "callable(f)" you can use
  "isinstance(f, collections.Callable)".  The "operator.isCallable()"
  function is also gone.

* Removed "coerce()".  This function no longer serves a purpose now
  that classic classes are gone.

* Removed "execfile()".  Instead of "execfile(fn)" use
  "exec(open(fn).read())".

* Removed the "file" type.  Use "open()".  There are now several
  different kinds of streams that open can return in the "io" module.

* Removed "reduce()".  Use "functools.reduce()" if you really need it;
  however, 99 percent of the time an explicit "for" loop is more
  readable.

* Removed "reload()".  Use "imp.reload()".

* Removed. "dict.has_key()" -- use the "in" operator instead.


构建和 C API 的改变
===================

Due to time constraints, here is a *very* incomplete list of changes
to the C API.

* Support for several platforms was dropped, including but not limited
  to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.

* **PEP 3118**: 新的缓冲区 API。

* **PEP 3121**: Extension Module Initialization & Finalization.

* **PEP 3123**: Making "PyObject_HEAD" conform to standard C.

* No more C API support for restricted execution.

* "PyNumber_Coerce()", "PyNumber_CoerceEx()", "PyMember_Get()", and
  "PyMember_Set()" C API 已被移除。

* New C API "PyImport_ImportModuleNoBlock()", works like
  "PyImport_ImportModule()" but won't block on the import lock
  (returning an error instead).

* Renamed the boolean conversion C-level slot and method: "nb_nonzero"
  is now "nb_bool".

* 从 C API 中移除 "METH_OLDARGS" 和 "WITH_CYCLE_GC"。


性能
====

The net result of the 3.0 generalizations is that Python 3.0 runs the
pystone benchmark around 10% slower than Python 2.5.  Most likely the
biggest cause is the removal of special-casing for small integers.
There's room for improvement, but it will happen after 3.0 is
released!


移植到 Python 3.0
=================

For porting existing Python 2.5 or 2.6 source code to Python 3.0, the
best strategy is the following:

* (Prerequisite:) Start with excellent test coverage.

* Port to Python 2.6.  This should be no more work than the average
  port from Python 2.x to Python 2.(x+1).  Make sure all your tests
  pass.

* (Still using 2.6:) Turn on the "-3" command line switch. This
  enables warnings about features that will be removed (or change) in
  3.0.  Run your test suite again, and fix code that you get warnings
  about until there are no warnings left, and all your tests still
  pass.

* Run the "2to3" source-to-source translator over your source code
  tree.  (See 2to3 --- 自动化的 Python 2 到 3 代码转写 for more on
  this tool.)  Run the result of the translation under Python 3.0.
  Manually fix up any remaining issues, fixing problems until all
  tests pass again.

It is not recommended to try to write source code that runs unchanged
under both Python 2.6 and 3.0; you'd have to use a very contorted
coding style, e.g. avoiding "print" statements, metaclasses, and much
more.  If you are maintaining a library that needs to support both
Python 2.6 and Python 3.0, the best approach is to modify step 3 above
by editing the 2.6 version of the source code and running the "2to3"
translator again, rather than editing the 3.0 version of the source
code.

For porting C extensions to Python 3.0, please see 将扩展模块移植到
Python 3.
