Python 2.7 Tutorial —— 类

来源:互联网 发布:matlab可视化编程 编辑:程序博客网 时间:2024/05/11 21:56

.. _tut-classes:

 

******************

Classes 类

******************

 

Python's class mechanism adds classes to the language with a minimum of new

syntax and semantics.  It is a mixture of the class mechanisms found in C++ and

Modula-3.  As is true for modules, classes in Python do not put an absolute

barrier between definition and user, but rather rely on the politeness of the

user not to "break into the definition."  The most important features of classes

are retained with full power, however: the class inheritance mechanism allows

multiple base classes, a derived class can override any methods of its base

class or classes, and a method can call the method of a base class with the same

name.  Objects can contain an arbitrary amount of data.

 

Python 在尽可能不增加新的语法和语义的情况下加入了类机制。这种机制是

C++ 和 Modula-3 的混合。像模块那样, Python 中的类没有在用户和定义之间建立一个绝对的

屏障,而是依赖于用户自觉的不去“破坏定义”。然而,类机制最重要的功能都

完整的保留下来:类继承机制允许多继承,派生类可以覆盖(override)基类中

的任何方法,方法中可以调用基类中的同名方法。对象可以包含任意数量的数据。

 

In C++ terminology, all class members (including the data members) are *public*,

and all member functions are *virtual*.  As in Modula-3, there are no shorthands

for referencing the object's members from its methods: the method function is

declared with an explicit first argument representing the object, which is

provided implicitly by the call.  As in Smalltalk, classes themselves are

objects.  This provides semantics for importing and renaming.  Unlike C++ and

Modula-3, built-in types can be used as base classes for extension by the user.

Also, like in C++, most built-in operators with special syntax (arithmetic

operators, subscripting etc.) can be redefined for class instances.

 

用 C++ 术语来讲,所有的类成员(包括数据成员)都是公有( public )的,

所有的成员函数都是 *虚* ( virtual )的。用

Modula-3的术语来讲,在成员方法中没有简便的方式引

用对象的成员:方法函数在定义时需要以引用的对象做为第一个参数,调用时则

会隐式引用对象。像在 Smalltalk 中一个,类也是对象。这就提供了导入和重

命名语义。不像 C++ 和 Modula-3 中

那样,大多数带有特殊语法的内置操作符(算法运算符、下标等)都可以针对类

的需要重新定义。

 

(Lacking universally accepted terminology to talk about classes, I will make

occasional use of Smalltalk and C++ terms.  I would use Modula-3 terms, since

its object-oriented semantics are closer to those of Python than C++, but I

expect that few readers have heard of it.)

 

(在讨论类时,没有足够的得到共识的术语,我会偶尔从 Smalltalk 和

C++ 借用一些。我比较喜欢用 Modula-3 的用语,因为比起C++, Python 的面

向对象语法更像它,但是我想很少有读者听过这个。)

 

.. _tut-object:

 

A Word About Names and Objects 关于命名和对象的内容

=============================================================

 

Objects have individuality, and multiple names (in multiple scopes) can be bound

to the same object.  This is known as aliasing in other languages.  This is

usually not appreciated on a first glance at Python, and can be safely ignored

when dealing with immutable basic types (numbers, strings, tuples).  However,

aliasing has a possibly surprising effect on the semantics of Python code

involving mutable objects such as lists, dictionaries, and most other types.

This is usually used to the benefit of the program, since aliases behave like

pointers in some respects.  For example, passing an object is cheap since only a

pointer is passed by the implementation; and if a function modifies an object

passed as an argument, the caller will see the change --- this eliminates the

need for two different argument passing mechanisms as in Pascal.

 

对象是被特化的,多个名字(在多个作用域中)可以绑定同一个对象。这相当于

其它语言中的别名。通常对 Python 的第一印象中会忽略这一点,使用那些不可

变的基本类型(数值、字符串、元组)时也可以很放心的忽视它。然而,在

Python 代码调用字典、链表之类可变对象,以及大多数涉及程序外部实体(文

件、窗体等等)的类型时,这一语义就会有影响。这通用有助于优化程序,因为

别名的行为在某些方面类似于指针。例如,很容易传递一个对象,因为在行为上

只是传递了一个指针。如果函数修改了一个通过参数传递的对象,调用者可以接

收到变化--在 Pascal 中这需要两个不同的参数传递机制。

 

.. _tut-scopes:

 

Python Scopes and Namespaces Python 作用域和命名空间

=====================================================================

 

Before introducing classes, I first have to tell you something about Python's

scope rules.  Class definitions play some neat tricks with namespaces, and you

need to know how scopes and namespaces work to fully understand what's going on.

Incidentally, knowledge about this subject is useful for any advanced Python

programmer.

 

在介绍类之前,我首先介绍一些有关 Python 作用域的规则。类的定义非常巧妙

的运用了命名空间,要完全理解接下来的知识,需要先理解作用域和命名空间的

工作原理。另外,这一切的知识对于任何高级 Python 程序员都非常有用。

 

Let's begin with some definitions.

 

我们从一些定义开始。

 

A *namespace* is a mapping from names to objects.  Most namespaces are currently

implemented as Python dictionaries, but that's normally not noticeable in any

way (except for performance), and it may change in the future.  Examples of

namespaces are: the set of built-in names (functions such as :func:`abs`, and

built-in exception names); the global names in a module; and the local names in

a function invocation.  In a sense the set of attributes of an object also form

a namespace.  The important thing to know about namespaces is that there is

absolutely no relation between names in different namespaces; for instance, two

different modules may both define a function ``maximize`` without confusion ---

users of the modules must prefix it with the module name.

 

*命名空间* 是从命名到对象的映射。当前命名空间主要是通过 Python 字典实现

的,不过通常不关心具体的实现方式(除非出于性能考虑),以后也有可能会改

变其实现方式。以下有一些命名空间的例子:内置命名(像 :func:`abs` 这样的函数,以

及内置异常名)集,模块中的全局命名,函数调用中的局部命名。某种意义上讲

对象的属性集也是一个命名空间。关于命名空间需要了解的一件很重要的事就是

不同命名空间中的命名没有任何联系,例如两个不同的模块可能都会定义一个名

为 ``maximize`` 的函数而不会发生混淆--用户必须以模块名为前缀来引用它们。

 

By the way, I use the word *attribute* for any name following a dot --- for

example, in the expression ``z.real``, ``real`` is an attribute of the object

``z``.  Strictly speaking, references to names in modules are attribute

references: in the expression ``modname.funcname``, ``modname`` is a module

object and ``funcname`` is an attribute of it.  In this case there happens to be

a straightforward mapping between the module's attributes and the global names

defined in the module: they share the same namespace!  [#]_

 

顺便提一句,我称 Python 中任何一个“.”之后的命名为 *属性* --例如,表达

式 ``z.real`` 中的 ``real`` 是对象 ``z`` 的一个属性。严格来讲,从模块中引用命名是

引用属性:表达式 ``modname.funcname`` 中, ``modname`` 是一个模块对象,funcname

是它的一个属性。因此,模块的属性和模块中的全局命名有直接的映射关系:它

们共享同一命名空间! [#]_

 

Attributes may be read-only or writable.  In the latter case, assignment to

attributes is possible.  Module attributes are writable: you can write

``modname.the_answer = 42``.  Writable attributes may also be deleted with the

:keyword:`del` statement.  For example, ``del modname.the_answer`` will remove

the attribute :attr:`the_answer` from the object named by ``modname``.

 

属性可以是只读过或写的。后一种情况下,可以对属性赋值。你可以这样

作: ``modname.the_answer = 42`` 。可写的属性也可以用 :keyword:`del` 语句删除。例

如: ``del modname.the_answer`` 会从 ``modname`` 对象中删除

:attr:`the_answer` 属性。

 

Namespaces are created at different moments and have different lifetimes.  The

namespace containing the built-in names is created when the Python interpreter

starts up, and is never deleted.  The global namespace for a module is created

when the module definition is read in; normally, module namespaces also last

until the interpreter quits.  The statements executed by the top-level

invocation of the interpreter, either read from a script file or interactively,

are considered part of a module called :mod:`__main__`, so they have their own

global namespace.  (The built-in names actually also live in a module; this is

called :mod:`__builtin__`.)

 

不同的命名空间在不同的时刻创建,有不同的生存期。包含内置命名的命名空间

在 Python 解释器启动时创建,会一直保留,不被删除。模块的全局命名空间在

模块定义被读入时创建,通常,模块命名空间也会一直保存到解释器退出。由解

释器在最高层调用执行的语句,不管它是从脚本文件中读入还是来自交互式输

入,都是 :mod:`__main__` 模块的一部分,所以它们也拥有自己的命名空间。(内置命

名也同样被包含在一个模块中,它被称作 :mod:`__builtin__` 。)

 

The local namespace for a function is created when the function is called, and

deleted when the function returns or raises an exception that is not handled

within the function.  (Actually, forgetting would be a better way to describe

what actually happens.)  Of course, recursive invocations each have their own

local namespace.

 

当函数被调用时创建一个局部命名空间,函数反正返回过抛出一个未在函数内处

理的异常时删除。(实际上,说是遗忘更为贴切)。当然,每一个递归调用拥有

自己的命名空间。

 

A *scope* is a textual region of a Python program where a namespace is directly

accessible.  "Directly accessible" here means that an unqualified reference to a

name attempts to find the name in the namespace.

 

*作用域* 是Python程序中一个命名空间可以直接访问的文法区域。“直接访问”

在这里的意思是查找命名时无需引用命名前缀。

 

Although scopes are determined statically, they are used dynamically. At any

time during execution, there are at least three nested scopes whose namespaces

are directly accessible:

 

尽管作用域是静态定义,在使用时他们都是动态的。每次执行时,至少有三个命

名空间可以直接访问的作用域嵌套在一起:包含局部命名的使用域在最里面,首

先被搜索;其次搜索的是中层的作用域,这里包含了同级的函数;最后搜索最外

面的作用域,它包含内置命名。

 

* the innermost scope, which is searched first, contains the local

  names

 

  首先搜索最内层的作用域,它包含局部命名

* the scopes of any enclosing functions, which are searched starting with the

  nearest enclosing scope, contains non-local, but also non-global

  names

 

  任意函数包含的作用域,是内层嵌套作用域搜索起点,包含非局部,但是也非

  全局的命名

* the next-to-last scope contains the current module's global names

 

  接下来的作用域包含当前模块的全局命名

* the outermost scope (searched last) is the namespace containing built-in names

 

  最外层的作用域(最后搜索)是包含内置命名的命名空间。

 

If a name is declared global, then all references and assignments go directly to

the middle scope containing the module's global names. Otherwise, all variables

found outside of the innermost scope are read-only (an attempt to write to such

a variable will simply create a *new* local variable in the innermost scope,

leaving the identically named outer variable unchanged).

 

如果一个命名声明为全局的,那么所有的赋值和引用都直接针对包含模全局命名

的中级作用域。另外,从外部访问到的所有内层作用域的变量都是只读的。(试

图写这样的变量只会在内部作用域创建一个 *新* 局部变量,外部标示命名的那

个变量不会改变)。

 

Usually, the local scope references the local names of the (textually) current

function.  Outside functions, the local scope references the same namespace as

the global scope: the module's namespace. Class definitions place yet another

namespace in the local scope.

 

通常,局部作用域引用当前函数的命名。在函数之外,局部作用域与

全局使用域引用同一命名空间:模块命名空间。类定义也是局部作用域中的另一

个命名空间。 

 

It is important to realize that scopes are determined textually: the global

scope of a function defined in a module is that module's namespace, no matter

from where or by what alias the function is called.  On the other hand, the

actual search for names is done dynamically, at run time --- however, the

language definition is evolving towards static name resolution, at "compile"

time, so don't rely on dynamic name resolution!  (In fact, local variables are

already determined statically.)

 

重要的是作用域决定于源程序的意义:一个定义于某模块中的函数的全局作用域

是该模块的命名空间,而不是该函数的别名被定义或调用的位置,了解这一点非

常重要。另一方面,命名的实际搜索过程是动态的,在运行时确定的——然

而,Python 语言也在不断发展,以后有可能会成为静态的“编译”时确定,所

以不要依赖动态解析!(事实上,局部变量已经是静态确定了。)

 

A special quirk of Python is that -- if no :keyword:`global` statement is in

effect -- assignments to names always go into the innermost scope.  Assignments

do not copy data --- they just bind names to objects.  The same is true for

deletions: the statement ``del x`` removes the binding of ``x`` from the

namespace referenced by the local scope.  In fact, all operations that introduce

new names use the local scope: in particular, :keyword:`import` statements and

function definitions bind the module or function name in the local scope.  (The

:keyword:`global` statement can be used to indicate that particular variables

live in the global scope.)

 

Python 的一个特别之处在于——如果没有使用 :keyword:`global` 语法——其赋值

操作总是在最里层的作用域。赋值不会复制数据——只是将命名绑定到对象。删除

也是如此: ``del x`` 只是从局部作用域的命名空间中删除命名 ``x`` 。事实

上,所有引入新命名的操作都作用于局部作用域。特别是 :keyword:`import`

语句和函数定将模块名或函数绑定于局部作用域。(可以使用

:keyword:`global` 语句将变量引入到全局作用域。)

 

.. _tut-firstclasses:

 

A First Look at Classes 初识类

===================================

 

Classes introduce a little bit of new syntax, three new object types, and some

new semantics.

 

类引入了一点新的语法,三种新的对象类型,以及一些新的语义。

 

.. _tut-classdefinition:

 

Class Definition Syntax 类定义语法

-----------------------------------------

 

The simplest form of class definition looks like this:

 

最简单的类定义形式如下 ::

 

   class ClassName:

       <statement-1>

       .

       .

       .

       <statement-N>

 

Class definitions, like function definitions (:keyword:`def` statements) must be

executed before they have any effect.  (You could conceivably place a class

definition in a branch of an :keyword:`if` statement, or inside a function.)

 

类的定义就像函数定义( :keyword:`def` 语句),要先执行才能生效。(你当然可以把它

放进 :keyword:`if` 语句的某一分支,或者一个函数的内部。) 

 

In practice, the statements inside a class definition will usually be function

definitions, but other statements are allowed, and sometimes useful --- we'll

come back to this later.  The function definitions inside a class normally have

a peculiar form of argument list, dictated by the calling conventions for

methods --- again, this is explained later.

 

习惯上,类定义语句的内容通常是函数定义,不过其它语句也可以,有时会很有

用——后面我们再回过头来讨论。类中的函数定义通常包括了一个特殊形式的参数

列表,用于方法调用约定——同样我们在后面讨论这些。

 

When a class definition is entered, a new namespace is created, and used as the

local scope --- thus, all assignments to local variables go into this new

namespace.  In particular, function definitions bind the name of the new

function here.

 

进入类定义部分后,会创建出一个新的命名空间,作为局部作用域——因此,所有

的赋值成为这个新命名空间的局部变量。特别是函数定义在此绑定了新的命名。

 

When a class definition is left normally (via the end), a *class object* is

created.  This is basically a wrapper around the contents of the namespace

created by the class definition; we'll learn more about class objects in the

next section.  The original local scope (the one in effect just before the class

definition was entered) is reinstated, and the class object is bound here to the

class name given in the class definition header (:class:`ClassName` in the

example).

 

类定义完成时(正常退出),就创建了一个 *类对象* 。基本上它是对类定义创建的

命名空间进行了一个包装;我们在下一节进一步学习类对象的知识。原始的局部

作用域(类定义引入之前生效的那个)得到恢复,类对象在这里绑定到类定义头

部的类名(例子中是 :class:`ClassName` )。 

 

.. _tut-classobjects:

 

Class Objects 类对象

----------------------------

 

Class objects support two kinds of operations: attribute references and

instantiation.

 

类对象支持两种操作:属性引用和实例化。

 

*Attribute references* use the standard syntax used for all attribute references

in Python: ``obj.name``.  Valid attribute names are all the names that were in

the class's namespace when the class object was created.  So, if the class

definition looked like this:

 

属性引用使用和 Python 中所有的属性引用一样的标准语法:obj.name。类对象

创建后,类命名空间中所有的命名都是有效属性名。所以如果类定义是这样 ::

 

   class MyClass:

       """A simple example class"""

       i = 12345

       def f(self):

           return 'hello world'

 

then ``MyClass.i`` and ``MyClass.f`` are valid attribute references, returning

an integer and a function object, respectively. Class attributes can also be

assigned to, so you can change the value of ``MyClass.i`` by assignment.

:attr:`__doc__` is also a valid attribute, returning the docstring belonging to

the class: ``"A simple example class"``.

 

那么 ``MyClass.i`` 和 ``MyClass.f`` 是有效的属性引用,分别返回一个整数和一个方

法对象。也可以对类属性赋值,你可以通过给 ``MyClass.i`` 赋值来修改它。

:attr:`__doc__`` 也是一个有效的属性,返回类的文档字符串: 

``"A simple example class"`` 。 

 

Class *instantiation* uses function notation.  Just pretend that the class

object is a parameterless function that returns a new instance of the class.

For example (assuming the above class):

 

类的 *实例化* 使用函数符号。只要将类对象看作是一个返回新的类实例的无参

数函数即可。例如(假设沿用前面的类) ::

 

   x = MyClass()

 

creates a new *instance* of the class and assigns this object to the local

variable ``x``.

 

以上创建了一个新的类 *实例* 并将该对象赋给局部变量 ``x`` 。

 

The instantiation operation ("calling" a class object) creates an empty object.

Many classes like to create objects with instances customized to a specific

initial state. Therefore a class may define a special method named

:meth:`__init__`, like this:

 

这个实例化操作(“调用”一个类对象)来创建一个空的对象。很多类都倾向于

将对象创建为有初始状态的。因此类可能会定义一个名为 :meth:`__init__` 的特殊方

法,像下面这样 ::

 

   def __init__(self):

       self.data = []

 

When a class defines an :meth:`__init__` method, class instantiation

automatically invokes :meth:`__init__` for the newly-created class instance.  So

in this example, a new, initialized instance can be obtained by:

 

类定义了 :meth:`__init__` 方法的话,类的实例化操作会自动为新创建的类实例调用

:meth:`__init__`` 方法。所以在下例中,可以这样创建一个新的实例 ::

 

   x = MyClass()

 

Of course, the :meth:`__init__` method may have arguments for greater

flexibility.  In that case, arguments given to the class instantiation operator

are passed on to :meth:`__init__`.  For example, :

 

当然,出于弹性的需要, :meth:`__init__` 方法可以有参数。事实上,参数通过

:meth:`__init__`` 传递到类的实例化操作上。例如 ::

 

   >>> class Complex:

   ...     def __init__(self, realpart, imagpart):

   ...         self.r = realpart

   ...         self.i = imagpart

   ...

   >>> x = Complex(3.0, -4.5)

   >>> x.r, x.i

   (3.0, -4.5)

 

 

.. _tut-instanceobjects:

 

Instance Objects 实例对象

--------------------------------

 

Now what can we do with instance objects?  The only operations understood by

instance objects are attribute references.  There are two kinds of valid

attribute names, data attributes and methods.

 

现在我们可以用实例对象作什么?实例对象唯一可用的操作就是属性引用。有两

种有效的属性名。

 

*data attributes* correspond to "instance variables" in Smalltalk, and to "data

members" in C++.  Data attributes need not be declared; like local variables,

they spring into existence when they are first assigned to.  For example, if

``x`` is the instance of :class:`MyClass` created above, the following piece of

code will print the value ``16``, without leaving a trace:

 

*数据属性* 相当于 Smalltalk 中的“实例变量”或 C++中的“数据成员”。和局

部变量一样,数据属性不需要声明,第一次使用时它们就会生成。例如,如果 x

是前面创建的 :class:`MyClass` 实例,下面这段代码会打印出 ``16`` 而在堆

栈中留下多余的东西 ::

 

   x.counter = 1

   while x.counter < 10:

       x.counter = x.counter * 2

   print x.counter

   del x.counter

 

The other kind of instance attribute reference is a *method*. A method is a

function that "belongs to" an object.  (In Python, the term method is not unique

to class instances: other object types can have methods as well.  For example,

list objects have methods called append, insert, remove, sort, and so on.

However, in the following discussion, we'll use the term method exclusively to

mean methods of class instance objects, unless explicitly stated otherwise.)

 

另一种为实例对象所接受的引用属性是 *方法* 。方法是“属于”一个对象的函数。

(在 Python 中,方法不止是类实例所独有:其它类型的对象也可有方法。例

如,链表对象有 append,insert,remove,sort 等等方法。然而,在后面的介

绍中,除非特别说明,我们提到的方法特指类方法)

 

.. index:: object: method

 

Valid method names of an instance object depend on its class.  By definition,

all attributes of a class that are function  objects define corresponding

methods of its instances.  So in our example, ``x.f`` is a valid method

reference, since ``MyClass.f`` is a function, but ``x.i`` is not, since

``MyClass.i`` is not.  But ``x.f`` is not the same thing as ``MyClass.f`` --- it

is a *method object*, not a function object.

 

实例对象的有效名称依赖于它的类。按照定义,类中所有(用户定义)的函数对

象对应它的实例中的方法。所以在我们的例子中,``x.f`` 是一个有效的方法引用,

因为 ``MyClass.f`` 是一个函数。但 ``x.i`` 不是,因为 ``MyClass.i`` 不是函数。不

过 ``x.f`` 和 ``MyClass.f`` 不同--它是一个 *方法对象* ,不是一个函数对象。 

 

.. _tut-methodobjects:

 

Method Objects 方法对象

-----------------------------

 

Usually, a method is called right after it is bound:

 

通常,方法通过右绑定调用 ::

 

   x.f()

 

In the :class:`MyClass` example, this will return the string ``'hello world'``.

However, it is not necessary to call a method right away: ``x.f`` is a method

object, and can be stored away and called at a later time.  For example:

 

在 :class:`MyClass` 示例中,这会返回字符串 ``'hello world'`` 。然而,也不是一定要直

接调用方法。 ``x.f`` 是一个方法对象,它可以存储起来以后调用。例如 ::

 

   xf = x.f

   while True:

       print xf()

 

will continue to print ``hello world`` until the end of time.

 

会不断的打印 "hello world" 。

 

What exactly happens when a method is called?  You may have noticed that

``x.f()`` was called without an argument above, even though the function

definition for :meth:`f` specified an argument.  What happened to the argument?

Surely Python raises an exception when a function that requires an argument is

called without any --- even if the argument isn't actually used...

 

调用方法时发生了什么?你可能注意到调用 ``x.f()`` 时没有引用前面标出的变量,尽

管在 :meth:`f` 的函数定义中指明了一个参数。这个参数怎么了?事实上如果函数调用

中缺少参数,Python 会抛出异常--甚至这个参数实际上没什么用……

 

Actually, you may have guessed the answer: the special thing about methods is

that the object is passed as the first argument of the function.  In our

example, the call ``x.f()`` is exactly equivalent to ``MyClass.f(x)``.  In

general, calling a method with a list of *n* arguments is equivalent to calling

the corresponding function with an argument list that is created by inserting

the method's object before the first argument.

 

实际上,你可能已经猜到了答案:方法的特别之处在于实例对象作为函数的第一

个参数传给了函数。在我们的例子中,调用 ``x.f()`` 相当于 ``MyClass.f(x)`` 。通

常,以 *n* 个参数的列表去调用一个方法就相当于将方法的对象插入到参数列表

的最前面后,以这个列表去调用相应的函数。

 

If you still don't understand how methods work, a look at the implementation can

perhaps clarify matters.  When an instance attribute is referenced that isn't a

data attribute, its class is searched.  If the name denotes a valid class

attribute that is a function object, a method object is created by packing

(pointers to) the instance object and the function object just found together in

an abstract object: this is the method object.  When the method object is called

with an argument list, a new argument list is constructed from the instance

object and the argument list, and the function object is called with this new

argument list.

 

如果你还是不理解方法的工作原理,了解一下它的实现也许有帮助。引用非数据

属性的实例属性时,会搜索它的类。如果这个命名确认为一个有效的函数对象类

属性,就会将实例对象和函数对象封装进一个抽象对象:这就是方法对象。以一

个参数列表调用方法对象时,它被重新拆封,用实例对象和原始的参数列表构造

一个新的参数列表,然后函数对象调用这个新的参数列表。

 

.. _tut-remarks:

 

Random Remarks 一些说明

===========================

 

.. These should perhaps be placed more carefully...

 

Data attributes override method attributes with the same name; to avoid

accidental name conflicts, which may cause hard-to-find bugs in large programs,

it is wise to use some kind of convention that minimizes the chance of

conflicts.  Possible conventions include capitalizing method names, prefixing

data attribute names with a small unique string (perhaps just an underscore), or

using verbs for methods and nouns for data attributes.

 

同名的数据属性会覆盖方法属性,为了避免可能的命名冲突--这在大型程序中

可能会导致难以发现的 bug --最好以某种命名约定来避免冲突。可选的约定

包括方法的首字母大写,数据属性名前缀小写(可能只是一个下划线),或者方

法使用动词而数据属性使用名词。

 

Data attributes may be referenced by methods as well as by ordinary users

("clients") of an object.  In other words, classes are not usable to implement

pure abstract data types.  In fact, nothing in Python makes it possible to

enforce data hiding --- it is all based upon convention.  (On the other hand,

the Python implementation, written in C, can completely hide implementation

details and control access to an object if necessary; this can be used by

extensions to Python written in C.)

 

数据属性可以由方法引用,也可以由普通用户(客户)调用。换句话说,类不能

实现纯抽象数据类型。事实上 Python 中没有什么办法可以强制隐藏数据--一切

都基本约定的惯例。(另一方法讲,Python 的实现是用 C 写成的,如果有必

要,可以用 C 来编写 Python 扩展,完全隐藏实现的细节,控制对象的访问。)

 

Clients should use data attributes with care --- clients may mess up invariants

maintained by the methods by stamping on their data attributes.  Note that

clients may add data attributes of their own to an instance object without

affecting the validity of the methods, as long as name conflicts are avoided ---

again, a naming convention can save a lot of headaches here.

 

客户应该小心使用数据属性--客户可能会因为随意修改数据属性而破坏了本来

由方法维护的数据一致性。需要注意的是,客户只要注意避免命名冲突,就可以

随意向实例中添加数据属性而不会影响方法的有效性--再次强调,命名约定可

以省去很多麻烦。

 

There is no shorthand for referencing data attributes (or other methods!) from

within methods.  I find that this actually increases the readability of methods:

there is no chance of confusing local variables and instance variables when

glancing through a method.

 

从方法内部引用数据属性(以及其它方法!)没有什么快捷的方式。我认为这事

实上增加了方法的可读性:即使粗略的浏览一个方法,也不会有混淆局部变量和

实例变量的机会。

 

Often, the first argument of a method is called ``self``.  This is nothing more

than a convention: the name ``self`` has absolutely no special meaning to

Python.  Note, however, that by not following the convention your code may be

less readable to other Python programmers, and it is also conceivable that a

*class browser* program might be written that relies upon such a convention.

 

通常方法的第一个参数命名为 ``self`` 。这仅仅是一个约定:对 Python 而言,

``self`` 绝对没有任何特殊含义。(然而要注意的是,如果不遵守这个约定,别的

Python 程序员阅读你的代码时会有不便,而且有些类浏览程序也是遵循此约定

开发的。)

 

Any function object that is a class attribute defines a method for instances of

that class.  It is not necessary that the function definition is textually

enclosed in the class definition: assigning a function object to a local

variable in the class is also ok.  For example:

 

类属性中的任何函数对象在类实例中都定义为方法。不是必须要将函数定义代码

写进类定义中,也可以将一个函数对象赋给类中的一个变量。例如 ::

 

   # Function defined outside the class

   def f1(self, x, y):

       return min(x, x+y)

 

   class C:

       f = f1

       def g(self):

           return 'hello world'

       h = g

 

Now ``f``, ``g`` and ``h`` are all attributes of class :class:`C` that refer to

function objects, and consequently they are all methods of instances of

:class:`C` --- ``h`` being exactly equivalent to ``g``.  Note that this practice

usually only serves to confuse the reader of a program.

 

现在 ``f``, ``g`` 和 ``h`` 都是类 :class:`C` 的属性,引用的都是函数对

象,因此它们都是 :class:`C` 实例的方法-- ``h`` 严格等于 ``g`` 。要注意的是这

种习惯通常只会迷惑程序的读者。

 

Methods may call other methods by using method attributes of the ``self``

argument:

 

通过 ``self`` 参数的方法属性,方法可以调用其它的方法 ::

 

   class Bag:

       def __init__(self):

           self.data = []

       def add(self, x):

           self.data.append(x)

       def addtwice(self, x):

           self.add(x)

           self.add(x)

 

Methods may reference global names in the same way as ordinary functions.  The

global scope associated with a method is the module containing the class

definition.  (The class itself is never used as a global scope.)  While one

rarely encounters a good reason for using global data in a method, there are

many legitimate uses of the global scope: for one thing, functions and modules

imported into the global scope can be used by methods, as well as functions and

classes defined in it.  Usually, the class containing the method is itself

defined in this global scope, and in the next section we'll find some good

reasons why a method would want to reference its own class.

 

方法可以像引用普通的函数那样引用全局命名。与方法关联的全局作用域是包含

类定义的模块。(类本身永远不会做为全局作用域使用。)尽管很少有好的理由

在方法中使用全局数据,全局作用域确有很多合法的用途:其一是方法可以调用

导入全局作用域的函数和方法,也可以调用定义在其中的类和函数。通常,包含

此方法的类也会定义在这个全局作用域,在下一节我们会了解为何一个方法要引

用自己的类。

 

Each value is an object, and therefore has a *class* (also called its *type*).

It is stored as ``object.__class__``.

 

每个值都是一个对象,因此每个值都有一个 *类(class)* (也称为它的 *类型(type)*

),它存储为 ``object.__class__`` 。

 

.. _tut-inheritance:

 

Inheritance 继承

=======================

 

Of course, a language feature would not be worthy of the name "class" without

supporting inheritance.  The syntax for a derived class definition looks like

this:

 

当然,如果一种语言不支持继承就,“类”就没有什么意义。派生类的定义如下

所示 ::

 

   class DerivedClassName(BaseClassName):

       <statement-1>

       .

       .

       .

       <statement-N>

 

The name :class:`BaseClassName` must be defined in a scope containing the

derived class definition.  In place of a base class name, other arbitrary

expressions are also allowed.  This can be useful, for example, when the base

class is defined in another module:

 

命名 :class:`BaseClassName` (示例中的基类名)必须与派生类定义在一个作用域内。除

了类,还可以用表达式,基类定义在另一个模块中时这一点非常有用 ::

 

   class DerivedClassName(modname.BaseClassName):

 

Execution of a derived class definition proceeds the same as for a base class.

When the class object is constructed, the base class is remembered.  This is

used for resolving attribute references: if a requested attribute is not found

in the class, the search proceeds to look in the base class.  This rule is

applied recursively if the base class itself is derived from some other class.

 

派生类定义的执行过程和基类是一样的。构造派生类对象时,就记住了基类。这

在解析属性引用的时候尤其有用:如果在类中找不到请求调用的属性,就搜索基

类。如果基类是由别的类派生而来,这个规则会递归的应用上去。

 

There's nothing special about instantiation of derived classes:

``DerivedClassName()`` creates a new instance of the class.  Method references

are resolved as follows: the corresponding class attribute is searched,

descending down the chain of base classes if necessary, and the method reference

is valid if this yields a function object.

 

派生类的实例化没有什么特殊之处: ``DerivedClassName()`` (示列中的派生类)

创建一个新的类实例。方法引用按如下规则解析:搜索对应的类属性,必要时沿

基类链逐级搜索,如果找到了函数对象这个方法引用就是合法的。

 

Derived classes may override methods of their base classes.  Because methods

have no special privileges when calling other methods of the same object, a

method of a base class that calls another method defined in the same base class

may end up calling a method of a derived class that overrides it.  (For C++

programmers: all methods in Python are effectively ``virtual``.)

 

派生类可能会覆盖其基类的方法。因为方法调用同一个对象中的其它方法时没有

特权,基类的方法调用同一个基类的方法时,可能实际上最终调用了派生类中的

覆盖方法。(对于 C++ 程序员来说,Python中的所有方法本质上都是 ``虚`` 方法。)

 

An overriding method in a derived class may in fact want to extend rather than

simply replace the base class method of the same name. There is a simple way to

call the base class method directly: just call ``BaseClassName.methodname(self,

arguments)``.  This is occasionally useful to clients as well.  (Note that this

only works if the base class is accessible as ``BaseClassName`` in the global

scope.)

 

派生类中的覆盖方法可能是想要扩充而不是简单的替代基类中的重名方法。有一

个简单的方法可以直接调用基类方法,只要调用: 

``BaseClassName.methodname(self, arguments)`` 。有时这对于客户也很有用。

(要注意只有 ``BaseClassName`` 在同一全局作用域定义或导入时才能这样用。)

 

Python has two built-in functions that work with inheritance:

 

Python 有两个用于继承的函数:

 

* Use :func:`isinstance` to check an instance's type: ``isinstance(obj, int)``

  will be ``True`` only if ``obj.__class__`` is :class:`int` or some class

  derived from :class:`int`.

 

  函数 :func:`isinstance` 用于检查实例类型: ``isinstance(obj, int)``

  只有在 ``obj.__class__`` 是 :class:`int` 或其它从 :class:`int` 继承

  的类型

 

* Use :func:`issubclass` to check class inheritance: ``issubclass(bool, int)``

  is ``True`` since :class:`bool` is a subclass of :class:`int`.  However,

  ``issubclass(unicode, str)`` is ``False`` since :class:`unicode` is not a

  subclass of :class:`str` (they only share a common ancestor,

  :class:`basestring`).

 

  函数 :func:`issubclass` 用于检查类继承: ``issubclass(bool, int)``

  为 ``True`` ,因为 :class:`bool` 是 :class:`int` 的子类。但是,

  ``issubclass(unicode, str)`` 是 ``False`` ,因为 :class:`unicode` 不

  是 :class:`str` 的子类(它们只是共享一个通用祖先类

  :class:`basestring` )。

 

.. _tut-multiple:

 

Multiple Inheritance 多继承

-------------------------------------

 

Python supports a limited form of multiple inheritance as well.  A class

definition with multiple base classes looks like this:

 

Python同样有限的支持多继承形式。多继承的类定义形如下例 ::

 

   class DerivedClassName(Base1, Base2, Base3):

       <statement-1>

       .

       .

       .

       <statement-N>

 

For old-style classes, the only rule is depth-first, left-to-right.  Thus, if an

attribute is not found in :class:`DerivedClassName`, it is searched in

:class:`Base1`, then (recursively) in the base classes of :class:`Base1`, and

only if it is not found there, it is searched in :class:`Base2`, and so on.

 

对于旧式的类,唯一的规则顺序是深度优先,从左到右。因此,如果在

:class:`DerivedClassName` (示例中的派生类)中没有找到某个属性,就会搜

索 :class:`Base1` ,然后(递归的)搜索其基类,如果最终没有找到,就搜索

:class:`Base2` ,以此类推。

 

(To some people breadth first --- searching :class:`Base2` and :class:`Base3`

before the base classes of :class:`Base1` --- looks more natural.  However, this

would require you to know whether a particular attribute of :class:`Base1` is

actually defined in :class:`Base1` or in one of its base classes before you can

figure out the consequences of a name conflict with an attribute of

:class:`Base2`.  The depth-first rule makes no differences between direct and

inherited attributes of :class:`Base1`.)

 

(有些人认为广度优先--在搜索 :class:`Base1` 的基类之前搜

索:class:`Base2` 和 :class:`Base3` --看起来更为自然。然而,如果

:class:`Base1` 和 :class:`Base2` 之间发生了命名冲突,你需要了解这个属性

是定义于 ::class:`Base1` 还是 :class:`Base1` 的基类中。而深度优先不区分

属性继承自基类还是直接定义。)

 

For :term:`new-style class`/es, the method resolution order changes dynamically

to support cooperative calls to :func:`super`.  This approach is known in some

other multiple-inheritance languages as call-next-method and is more powerful

than the super call found in single-inheritance languages.

 

对于 :term:`new-style class` 类, :func:`super` 可以动态的改变解析顺

序。这个方式可见于其它的一些多继承语言,类似 call-next-method,比单继

承语言中的 super 更强大 :

 

With new-style classes, dynamic ordering is necessary because all  cases of

multiple inheritance exhibit one or more diamond relationships (where one at

least one of the parent classes can be accessed through multiple paths from the

bottommost class).  For example, all new-style classes inherit from

:class:`object`, so any case of multiple inheritance provides more than one path

to reach :class:`object`.  To keep the base classes from being accessed more

than once, the dynamic algorithm linearizes the search order in a way that

preserves the left-to-right ordering specified in each class, that calls each

parent only once, and that is monotonic (meaning that a class can be subclassed

without affecting the precedence order of its parents).  Taken together, these

properties make it possible to design reliable and extensible classes with

multiple inheritance.  For more detail, see

http://www.python.org/download/releases/2.3/mro/ .

 

在 new-style 类中,必须有动态调整顺序,因为所有的多继承会有一到多个菱

形关系(指有至少一个祖先类可以从子类经由多个继承路径到达)。例如,所有

的 new-style 类继承自 :class:`object` ,所以任意的多继承总是会有多于一

条继承路径到达 :class:`object` 。为了防止重复访问基类,通过动态的线性化

算法,每个类都按从左到右的顺序特别指定了顺序,每个祖先类只调用一次,这

是单调的(意味着一个类被继承时不会影响它祖先的次序)。总算可以通过这种

方式使得设计一个可靠并且可扩展的多继承类成为可能。进一步的内容请参见 

http://www.python.org/download/releases/2.3/mro/ 。

 

.. _tut-private:

 

Private Variables 私有变量

===================================

 

"Private" instance variables that cannot be accessed except from inside an

object don't exist in Python.  However, there is a convention that is followed

by most Python code: a name prefixed with an underscore (e.g. ``_spam``) should

be treated as a non-public part of the API (whether it is a function, a method

or a data member).  It should be considered an implementation detail and subject

to change without notice.

 

只能从对像内部访问的“私有”实例变量,在 Python 中不存在。然而,也有

一个变通的访问用于大多数 Python 代码:以一个下划线开头的命名(例如

``_spam`` )会被处理为 API 的非公开部分(无论它是一个函数、方法或数据

成员)。它会被视为一个实现细节,无需公开。

 

Since there is a valid use-case for class-private members (namely to avoid name

clashes of names with names defined by subclasses), there is limited support for

such a mechanism, called :dfn:`name mangling`.  Any identifier of the form

``__spam`` (at least two leading underscores, at most one trailing underscore)

is textually replaced with ``_classname__spam``, where ``classname`` is the

current class name with leading underscore(s) stripped.  This mangling is done

without regard to the syntactic position of the identifier, as long as it

occurs within the definition of a class.

 

因为有一个正当的类私有成员用途(即避免子类里定义的命名与之冲突),Python

提供了对这种结构的有限支持,称为 :dfn:`name mangling(命名编码)` 。任何形如

``__spam`` 的标识(前面至少两个下划线,后面至多一个),被替代为

``_classname__spam`` ,去掉前导下划线的 ``classname`` 即当前的类名。此

语法不关注标识的位置,只要求在类定义内。

 

Note that the mangling rules are designed mostly to avoid accidents; it still is

possible to access or modify a variable that is considered private.  This can

even be useful in special circumstances, such as in the debugger.

 

需要注意的是编码规则设计为尽可能的避免冲突,被认作为私有的变量仍然有可

能被访问或修改。在特定的场合它也是有用的,比如调试的时候。

 

Notice that code passed to ``exec``, ``eval()`` or ``execfile()`` does not

consider the classname of the invoking  class to be the current class; this is

similar to the effect of the  ``global`` statement, the effect of which is

likewise restricted to  code that is byte-compiled together.  The same

restriction applies to ``getattr()``, ``setattr()`` and ``delattr()``, as well

as when referencing ``__dict__`` directly.

 

要注意的是代码传入 ``exec`` , ``eval()`` 或 ``execfile()`` 时不考虑所调

用的类的类名,视其为当前类,这类似于 ``global`` 语句的效应,

已经按字节编译的部分也有同样的限制。这也同样作用于 ``getattr()`` ,

``setattr()`` 和 ``delattr`` ,像直接引用 ``__dict__`` 一样。

 

.. _tut-odds:

 

Odds and Ends 补充

========================

 

Sometimes it is useful to have a data type similar to the Pascal "record" or C

"struct", bundling together a few named data items.  An empty class definition

will do nicely:

 

有时类似于Pascal中“记录(record)”或C中“结构(struct)”的数据类型

很有用,它将一组已命名的数据项绑定在一起。一个空的类定义可以很好的实现

这它 ::

 

   class Employee:

       pass

 

   john = Employee() # Create an empty employee record

 

   # Fill the fields of the record

   john.name = 'John Doe'

   john.dept = 'computer lab'

   john.salary = 1000

 

A piece of Python code that expects a particular abstract data type can often be

passed a class that emulates the methods of that data type instead.  For

instance, if you have a function that formats some data from a file object, you

can define a class with methods :meth:`read` and :meth:`readline` that get the

data from a string buffer instead, and pass it as an argument.

 

某一段 Python 代码需要一个特殊的抽象数据结构的话,通常可以传入一个类,事实上这模仿了该类的方法。例如,如果你有一个用于从文件对象中格式化数据的函数,你可以定义一个带有 :meth:`read` 和 :meth:`readline` 方法的类,以此从字符串缓冲读取数据,然后将该类的对象作为参数传入前述的函数。

 

.. (Unfortunately, this technique has its limitations: a class can't define

   operations that are accessed by special syntax such as sequence subscripting

   or arithmetic operators, and assigning such a "pseudo-file" to sys.stdin will

   not cause the interpreter to read further input from it.)

 

Instance method objects have attributes, too: ``m.im_self`` is the instance

object with the method :meth:`m`, and ``m.im_func`` is the function object

corresponding to the method.

 

实例方法对象也有属性: ``m.im_self`` 是一个实例方法所属的对象,而 ``m.im_func`` 是这个方法对应的函数对象。

 

.. _tut-exceptionclasses:

 

Exceptions Are Classes Too 异常也是类

===========================================

 

User-defined exceptions are identified by classes as well.  Using this mechanism

it is possible to create extensible hierarchies of exceptions.

 

用户自定义异常也可以是类。利用这个机制可以创建可扩展的异常体系。

 

There are two new valid (semantic) forms for the :keyword:`raise` statement:

 

以下是两种新的,有效的(语义上的)异常抛出形式,使用 :keyword:`raise` 语句 ::

 

   raise Class, instance

 

   raise instance

 

In the first form, ``instance`` must be an instance of :class:`Class` or of a

class derived from it.  The second form is a shorthand for:

 

第一种形式中, ``instance`` 必须是 :class:`Class` 或其派生类的一个实例。

第二种形式是以下形式的简写 ::

 

   raise instance.__class__, instance

 

A class in an :keyword:`except` clause is compatible with an exception if it is

the same class or a base class thereof (but not the other way around --- an

except clause listing a derived class is not compatible with a base class).  For

example, the following code will print B, C, D in that order:

 

发生的异常其类型如果是 :keyword:`except` 子句中列出的类,或者是其派生

类,那么它们就是相符的(反过来说--发生的异常其类型如果是异常子句中列

出的类的基类,它们就不相符)。例如,以下代码会按顺序打印B,C,D ::

 

   class B:

       pass

   class C(B):

       pass

   class D(C):

       pass

 

   for c in [B, C, D]:

       try:

           raise c()

       except D:

           print "D"

       except C:

           print "C"

       except B:

           print "B"

 

Note that if the except clauses were reversed (with ``except B`` first), it

would have printed B, B, B --- the first matching except clause is triggered.

 

要注意的是如果异常子句的顺序颠倒过来( ``execpt B`` 在最前),它就会打印

B,B,B--第一个匹配的异常被触发。

 

When an error message is printed for an unhandled exception, the exception's

class name is printed, then a colon and a space, and finally the instance

converted to a string using the built-in function :func:`str`.

 

打印一个异常类的错误信息时,先打印类名,然后是一个空格、一个冒号,然后

是用内置函数 :func:`str` 将类转换得到的完整字符串。

 

.. _tut-iterators:

 

Iterators 迭代器

=========================

 

By now you have probably noticed that most container objects can be looped over

using a :keyword:`for` statement:

 

现在你可能注意到大多数容器对象都可以用 :keyword:`for` 遍历 ::

 

   for element in [1, 2, 3]:

       print element

   for element in (1, 2, 3):

       print element

   for key in {'one':1, 'two':2}:

       print key

   for char in "123":

       print char

   for line in open("myfile.txt"):

       print line

 

This style of access is clear, concise, and convenient.  The use of iterators

pervades and unifies Python.  Behind the scenes, the :keyword:`for` statement

calls :func:`iter` on the container object.  The function returns an iterator

object that defines the method :meth:`next` which accesses elements in the

container one at a time.  When there are no more elements, :meth:`next` raises a

:exc:`StopIteration` exception which tells the :keyword:`for` loop to terminate.

This example shows how it all works:

 

这种形式的访问清晰、简洁、方便。迭代器的用法在 Python 中普遍而且统一。

在后台, :keyword:`for` 语句在容器对象中调用 :func:`iter` 。 该函数返

回一个定义了 :meth:`next` 方法的迭代器对象,它在容器中逐一访问元素。没

有后续的元素时, :meth:`next` 抛出一个 :exc:`StopIteration` 异常通知

:keyword:`for` 语句循环结束。以下是其工作原理的示例 ::

 

   >>> s = 'abc'

   >>> it = iter(s)

   >>> it

   <iterator object at 0x00A1DB50>

   >>> it.next()

   'a'

   >>> it.next()

   'b'

   >>> it.next()

   'c'

   >>> it.next()

 

   Traceback (most recent call last):

     File "<stdin>", line 1, in ?

       it.next()

   StopIteration

 

Having seen the mechanics behind the iterator protocol, it is easy to add

iterator behavior to your classes.  Define a :meth:`__iter__` method which

returns an object with a :meth:`next` method.  If the class defines

:meth:`next`, then :meth:`__iter__` can just return ``self``:

 

了解了迭代器协议的后台机制,就可以很容易的给自己的类添加迭代器行为。定

义一个 :meth:`__iter__` 方法,使其返回一个带有 :meth:`next` 方法的对象。如果这个类

已经定义了 :meth:`next` ,那么 :meth:`__iter__` 只需要返回 ``self`` ::

 

   class Reverse:

       "Iterator for looping over a sequence backwards"

       def __init__(self, data):

           self.data = data

           self.index = len(data)

       def __iter__(self):

           return self

       def next(self):

           if self.index == 0:

               raise StopIteration

           self.index = self.index - 1

           return self.data[self.index]

 

   >>> for char in Reverse('spam'):

   ...     print char

   ...

   m

   a

   p

   s

 

 

.. _tut-generators:

 

Generators 生成器

=========================

 

:term:`Generator`/s are a simple and powerful tool for creating iterators.  They

are written like regular functions but use the :keyword:`yield` statement

whenever they want to return data.  Each time :meth:`next` is called, the

generator resumes where it left-off (it remembers all the data values and which

statement was last executed).  An example shows that generators can be trivially

easy to create:

 

:term:`生成器` 是创建迭代器的简单而强大的工具。它们写起来就像是正规的函

数,需要返回数据的时候使用 :keyword:`yield` 语句。每次 :meth:`next` 被

调用时,生成器回复它脱离的位置(它记忆语句最后一次执行的位置和所有的数

据值)。以下示例演示了生成器可以很简单的创建出来 ::

 

   def reverse(data):

       for index in range(len(data)-1, -1, -1):

           yield data[index]

 

   >>> for char in reverse('golf'):

   ...     print char

   ...

   f

   l

   o

   g

 

Anything that can be done with generators can also be done with class based

iterators as described in the previous section.  What makes generators so

compact is that the :meth:`__iter__` and :meth:`next` methods are created

automatically.

 

前一节中描述了基于类的迭代器,它能作的每一件事生成器也能作到。因为自动

创建了 :meth:`__iter__` 和 :meth:`next` 方法,生成器显得如此简洁。

 

Another key feature is that the local variables and execution state are

automatically saved between calls.  This made the function easier to write and

much more clear than an approach using instance variables like ``self.index``

and ``self.data``.

 

另一个关键的功能在于两次执行之间,局部变量和执行状态都自动的保存下来。

这使函数很容易写,而且比使用 ``self.index`` 和 ``self.data`` 之类的方

式更清晰。

 

In addition to automatic method creation and saving program state, when

generators terminate, they automatically raise :exc:`StopIteration`. In

combination, these features make it easy to create iterators with no more effort

than writing a regular function.

 

除了创建和保存程序状态的自动方法,当发生器终结时,还会自动抛出

:exc:`StopIteration` 异常。综上所述,这些功能使得编写一个正规函数成为

创建迭代器的最简单方法。

 

.. _tut-genexps:

 

Generator Expressions 生成器表达式

=======================================

 

Some simple generators can be coded succinctly as expressions using a syntax

similar to list comprehensions but with parentheses instead of brackets.  These

expressions are designed for situations where the generator is used right away

by an enclosing function.  Generator expressions are more compact but less

versatile than full generator definitions and tend to be more memory friendly

than equivalent list comprehensions.

 

有时简单的生成器可以用简洁的方式调用,就像不带中括号的链表推导式。这些

表达式是为函数调用生成器而设计的。生成器表达式比完整的生成器定义更简

洁,但是没有那么多变,而且通常比等价的链表推导式更容易记。

 

Examples:

 

例如 ::

 

   >>> sum(i*i for i in range(10))                 # sum of squares

   285

 

   >>> xvec = [10, 20, 30]

   >>> yvec = [7, 5, 3]

   >>> sum(x*y for x,y in zip(xvec, yvec))         # dot product

   260

 

   >>> from math import pi, sin

   >>> sine_table = dict((x, sin(x*pi/180)) for x in range(0, 91))

 

   >>> unique_words = set(word  for line in page  for word in line.split())

 

   >>> valedictorian = max((student.gpa, student.name) for student in graduates)

 

   >>> data = 'golf'

   >>> list(data[i] for i in range(len(data)-1,-1,-1))

   ['f', 'l', 'o', 'g']

 

 

 

.. rubric:: Footnotes

 

.. [#] Except for one thing.  Module objects have a secret read-only attribute called

   :attr:`__dict__` which returns the dictionary used to implement the module's

   namespace; the name :attr:`__dict__` is an attribute but not a global name.

   Obviously, using this violates the abstraction of namespace implementation, and

   should be restricted to things like post-mortem debuggers.

 

.. [#] 有一个例外。模块对象有一个隐秘的只读对象,名为 :attr:`__dict__` ,它

   返回用于实现模块命名空间的字典,命名 :attr:`__dict__` 是一个属性而非

   全局命名。显然,使用它违反了命名空间实现的抽象原则,应该被严格限制于

   调试中。