📄 node11.html
字号:
<P>A <em>scope</em> 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.<P>尽管作用域是静态定义,在使用时他们都是动态的。每次执行时,至少有三个命名空间可以直接访问的作用域嵌套在一起:包含局部命名的使用域在最里面,首先被搜索;其次搜索的是中层的作用域,这里包含了同级的函数;最后搜索最外面的作用域,它包含内置命名。<P>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 namespaces of any enclosing
functions, which are searched starting with the nearest enclosing scope;
the middle scope, searched next, contains the current module's global names;
and the outermost scope (searched last) is the namespace containing built-in
names.<P>尽管作用域是静态定义,在使用时他们都是动态的。每次执行时,至少有三个命名空间可以直接访问的作用域嵌套在一起:包含局部命名的使用域在最里面,首先被搜索;其次搜索的是中层的作用域,这里包含了同级的函数;最后搜索最外面的作用域,它包含内置命名。<P>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.<P>如果一个命名声明为全局的,那么所有的赋值和引用都直接针对包含模全局命名的中级作用域。另外,从外部访问到的所有内层作用域的变量都是只读的。<P>Usually, the local scope references the local names of the (textually)
current function. Outside of 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.<P>从文本意义上讲,局部作用域引用当前函数的命名。在函数之外,局部作用域与全局使用域引用同一命名空间:模块命名空间。类定义也是局部作用域中的另一个命名空间。<P>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.)<P>作用域决定于源程序的文本:一个定义于某模块中的函数的全局作用域是该模块的命名空间,而不是该函数的别名被定义或调用的位置,了解这一点非常重要。另一方面,命名的实际搜索过程是动态的,在运行时确定的——然而,Python 语言也在不断发展,以后有可能会成为静态的“编译”时确定,所以不要依赖于动态解析!(事实上,局部变量已经是静态确定了。)<P>A special quirk of Python is that assignments 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
"<tt class="samp">del x</tt>" removes the binding of <code>x</code> from the namespace
referenced by the local scope. In fact, all operations that introduce
new names use the local scope: in particular, import statements and
function definitions bind the module or function name in the local
scope. (The <tt class="keyword">global</tt> statement can be used to indicate that
particular variables live in the global scope.)<P>Python 的一个特别之处在于其赋值操作总是在最里层的作用域。赋值不会复制数据——只是将命名绑定到对象。删除也是如此:"<tt class="samp">del
x</tt>" 只是从局部作用域的命名空间中删除命名 <code>x</code> 。事实上,所有引入新命名的操作都作用于局部作用域。特别是 import 语句和函数定将模块名或函数绑定于局部作用域。(可以使用 global 语句将变量引入到全局作用域。)<P><H1><A NAME="SECTION0011300000000000000000"></A><A NAME="firstClasses"></A><BR>9.3 初识类 A First Look at Classes </H1><P>Classes introduce a little bit of new syntax, three new object types,
and some new semantics.<P>类引入了一点新的语法,三种新的对象类型,以及一些新的语义。<P><H2><A NAME="SECTION0011310000000000000000"></A><A NAME="classDefinition"></A><BR>9.3.1 类定义语法 Class Definition Syntax </H2><P>The simplest form of class definition looks like this:<P>最简单的类定义形式如下:<P><div class="verbatim"><pre>
class ClassName:
<statement-1>
.
.
.
<statement-N></pre></div><P>Class definitions, like function definitions
(<tt class="keyword">def</tt> statements) must be executed before they have any
effect. (You could conceivably place a class definition in a branch
of an <tt class="keyword">if</tt> statement, or inside a function.)<P>类的定义就像函数定义( <tt class="keyword">def</tt> 语句),要先执行才能生效。(你当然可以把它放进 <tt class="keyword">if</tt> 语句的某一分支,或者一个函数的内部。)<P>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.<P>习惯上,类定义语句的内容通常是函数定义,不过其它语句也可以,有时会很有用——后面我们再回过头来讨论。类中的函数定义通常包括了一个特殊形式的参数列表,用于方法调用约定——同样我们在后面讨论这些。<P>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.<P>定义一个类的时候,会创建一个新的命名空间,将其作为局部作用域使用——因此,所以对局部变量的赋值都引入新的命名空间。特别是函数定义将新函数的命名绑定于此。<P>When a class definition is left normally (via the end), a <em>class
object</em> 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 definitions was entered) is
reinstated, and the class object is bound here to the class name given
in the class definition header (<tt class="class">ClassName</tt> in the example).<P>类定义完成时(正常退出),就创建了一个<em>类对象</em>。基本上它是对类定义创建的命名空间进行了一个包装;我们在下一节进一步学习类对象的知识。原始的局部作用域(类定义引入之前生效的那个)得到恢复,类对象在这里绑定到类定义头部的类名(例子中是 <tt class="class">ClassName</tt> )。<P><H2><A NAME="SECTION0011320000000000000000"></A><A NAME="classObjects"></A><BR>9.3.2 类对象 Class Objects </H2><P>Class objects support two kinds of operations: attribute references
and instantiation.<P>类对象支持两种操作:属性引用和实例化。<P><em>Attribute references</em> use the standard syntax used for all
attribute references in Python: <code>obj.name</code>. 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:<P>属性引用使用和 Python 中所有的属性引用一样的标准语法:<code>obj.name</code>。类对象创建后,类命名空间中所有的命名都是有效属性名。所以如果类定义是这样:<P><div class="verbatim"><pre>
class MyClass:
"A simple example class"
i = 12345
def f(self):
return 'hello world'</pre></div><P>then <code>MyClass.i</code> and <code>MyClass.f</code> are valid attribute
references, returning an integer and a method object, respectively.
Class attributes can also be assigned to, so you can change the value
of <code>MyClass.i</code> by assignment. <tt class="member">__doc__</tt> is also a valid
attribute, returning the docstring belonging to the class: <code>"A
simple example class"</code>.<P>那么 <code>MyClass.i</code> 和 <code>MyClass.f</code> 是有效的属性引用,分别返回一个整数和一个方法对象。也可以对类属性赋值,你可以通过给<code>MyClass.i</code> 赋值来修改它。 <tt class="member">__doc__</tt> 也是一个有效的属性,返回类的文档字符串: <code>"A simple example class"</code>。<P>Class <em>instantiation</em> 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):<P>类的实例化使用函数符号。只要将类对象看作是一个返回新的类实例的无参数函数即可。例如(假设沿用前面的类):<P><div class="verbatim"><pre>
x = MyClass()</pre></div><P>creates a new <em>instance</em> of the class and assigns this object to
the local variable <code>x</code>.<P>以上创建了一个新的类<em>实例</em>并将该对象赋给局部变量 <code>x</code>。<P>The instantiation operation (``calling'' a class object) creates an
empty object. Many classes like to create objects in a known initial
state. Therefore a class may define a special method named
<tt class="method">__init__()</tt>, like this:<P>这个实例化操作(“调用”一个类对象)来创建一个空的对象。很多类都倾向于将对象创建为有初始状态的。因此类可能会定义一个名为
<tt class="method">__init__()</tt> 的特殊方法,像下面这样:<P><div class="verbatim"><pre>
def __init__(self):
self.data = []</pre></div><P>When a class defines an <tt class="method">__init__()</tt> method, class
instantiation automatically invokes <tt class="method">__init__()</tt> for the
newly-created class instance. So in this example, a new, initialized
instance can be obtained by:<P>类定义了 <tt class="method">__init__()</tt> 方法的话,类的实例化操作会自动为新创建的类实例调用 <tt class="method">__init__()</tt> 方法。所以在下例中,可以这样创建一个新的实例:<P><div class="verbatim"><pre>
x = MyClass()</pre></div><P>Of course, the <tt class="method">__init__()</tt> method may have arguments for
greater flexibility. In that case, arguments given to the class
instantiation operator are passed on to <tt class="method">__init__()</tt>. For
example,<P>当然,出于弹性的需要, <tt class="method">__init__()</tt> 方法可以有参数。事实上,参数通过 <tt class="method">__init__()</tt> 传递到类的实例化操作上。例如:<P><div class="verbatim"><pre>
>>> 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)</pre></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -