⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node11.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
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!<P>方法可以像引用普通的函数那样引用全局命名。与方法关联的全局作用域是包含类定义的模块。(类本身永远不会做为全局作用域使用!)尽管很少有好的理由在方法中使用全局数据,全局作用域确有很多合法的用途:其一是方法可以调用导入全局作用域的函数和方法,也可以调用定义在其中的类和函数。通常,包含此方法的类也会定义在这个全局作用域,在下一节我们会了解为何一个方法要引用自己的类!<P><H1><A NAME="SECTION0011500000000000000000"></A><A NAME="inheritance"></A><BR>9.5 继承 Inheritance </H1><P>Of course, a language feature would not be worthy of the name ``class''
without supporting inheritance.  The syntax for a derived class
definition looks as follows:<P>当然,如果一种语言不支持继承就,“类”就没有什么意义。派生类的定义如下所示:<P><div class="verbatim"><pre>
class DerivedClassName(BaseClassName):
    &lt;statement-1&gt;
    .
    .
    .
    &lt;statement-N&gt;</pre></div><P>The name <tt class="class">BaseClassName</tt> must be defined in a scope containing
the derived class definition.  Instead of a base class name, an
expression is also allowed.  This is useful when the base class is
defined in another module,<P>命名 <tt class="class">BaseClassName</tt>(示例中的基类名)必须与派生类定义在一个作用域内。除了类,还可以用表达式,基类定义在另一个模块中时这一点非常有用:<P><div class="verbatim"><pre>
class DerivedClassName(modname.BaseClassName):</pre></div><P>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, it is searched in the
base class.  This rule is applied recursively if the base class itself
is derived from some other class.<P>派生类定义的执行过程和基类是一样的。构造派生类对象时,就记住了基类。这在解析属性引用的时候尤其有用:如果在类中找不到请求调用的属性,就搜索基类。如果基类是由别的类派生而来,这个规则会递归的应用上去。<P>There's nothing special about instantiation of derived classes:
<code>DerivedClassName()</code> 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.<P>派生类的实例化没有什么特殊之处:<code>DerivedClassName()</code> (示列中的派生类)创建一个新的类实例。方法引用按如下规则解析:搜索对应的类属性,必要时沿基类链逐级搜索,如果找到了函数对象这个方法引用就是合法的<P>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 in fact end up calling a method of
a derived class that overrides it.  (For C++ programmers: all methods
in Python are effectively <tt class="keyword">virtual</tt>.)<P>派生类可能会覆盖其基类的方法。因为方法调用同一个对象中的其它方法时没有特权,基类的方法调用同一个基类的方法时,可能实际上最终调用了派生类中的覆盖方法。(对于 C++ 程序员来说,Python中的所有方法本质上都是虚方法。)<P>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 "<tt class="samp">BaseClassName.methodname(self, arguments)</tt>".  This is
occasionally useful to clients as well.  (Note that this only works if
the base class is defined or imported directly in the global scope.)<P>派生类中的覆盖方法可能是想要扩充而不是简单的替代基类中的重名方法。有一个简单的方法可以直接调用基类方法,只要调用:"<tt class="samp">BaseClassName.methodname(self, arguments)</tt>"。有时这对于客户也很有用。(要注意的中只有基类在同一全局作用域定义或导入时才能这样用。)<P><H2><A NAME="SECTION0011510000000000000000"></A><A NAME="multiple"></A><BR>9.5.1 多继承 Multiple Inheritance </H2><P>Python supports a limited form of multiple inheritance as well.  A
class definition with multiple base classes looks as follows:<P>Python同样有限的支持多继承形式。多继承的类定义形如下例:<P><div class="verbatim"><pre>
class DerivedClassName(Base1, Base2, Base3):
    &lt;statement-1&gt;
    .
    .
    .
    &lt;statement-N&gt;</pre></div><P>The only rule necessary to explain the semantics is the resolution
rule used for class attribute references.  This is depth-first,
left-to-right.  Thus, if an attribute is not found in
<tt class="class">DerivedClassName</tt>, it is searched in <tt class="class">Base1</tt>, then
(recursively) in the base classes of <tt class="class">Base1</tt>, and only if it is
not found there, it is searched in <tt class="class">Base2</tt>, and so on.<P>这里唯一需要解释的语义是解析类属性的规则。顺序是深度优先,从左到右。因此,如果在 <tt class="class">DerivedClassName</tt> (示例中的派生类)中没有找到某个属性,就会搜索 <tt class="class">Base1</tt> ,然后(递归的)搜索其基类,如果最终没有找到,就搜索 <tt class="class">Base2</tt>,以此类推。<P>(To some people breadth first -- searching <tt class="class">Base2</tt> and
<tt class="class">Base3</tt> before the base classes of <tt class="class">Base1</tt> -- looks more
natural.  However, this would require you to know whether a particular
attribute of <tt class="class">Base1</tt> is actually defined in <tt class="class">Base1</tt> or in
one of its base classes before you can figure out the consequences of
a name conflict with an attribute of <tt class="class">Base2</tt>.  The depth-first
rule makes no differences between direct and inherited attributes of
<tt class="class">Base1</tt>.)<P>(有些人认为广度优先--在搜索<tt class="class">Base1</tt>的基类之前搜索<tt class="class">Base2</tt>和<code>Base3</code>--看起来更为自然。然而,如果<tt class="class">Base1</tt>和<tt class="class">Base2</tt>之间发生了命名冲突,你需要了解这个属性是定义于<tt class="class">Base1</tt>还是<tt class="class">Base1</tt>的基类中。而深度优先不区分属性继承自基类还是直接定义。)<P>It is clear that indiscriminate use of multiple inheritance is a
maintenance nightmare, given the reliance in Python on conventions to
avoid accidental name conflicts.  A well-known problem with multiple
inheritance is a class derived from two classes that happen to have a
common base class.  While it is easy enough to figure out what happens
in this case (the instance will have a single copy of ``instance
variables'' or data attributes used by the common base class), it is
not clear that these semantics are in any way useful.<P>显然不加限制的使用多继承会带来维护上的噩梦,因为 Python 中只依靠约定来避免命名冲突。多继承一个很有名的问题是派生继承的两个基类都是从同一个基类继承而来。目前还不清楚这在语义上有什么意义,然而很容易想到这会造成什么后果(实例会有一个独立的“实例变量”或数据属性复本作用于公共基类。)<P><H1><A NAME="SECTION0011600000000000000000"></A><A NAME="private"></A><BR>9.6 私有变量 Private Variables </H1><P>There is limited support for class-private
identifiers.  Any identifier of the form <code>__spam</code> (at least two
leading underscores, at most one trailing underscore) is now textually
replaced with <code>_classname__spam</code>, where <code>classname</code> is the
current class name with leading underscore(s) stripped.  This mangling
is done without regard of the syntactic position of the identifier, so
it can be used to define class-private instance and class variables,
methods, as well as globals, and even to store instance variables
private to this class on instances of <em>other</em> classes.  Truncation
may occur when the mangled name would be longer than 255 characters.
Outside classes, or when the class name consists of only underscores,
no mangling occurs.<P>Python 对类的私有成员提供了有限的支持。任何形如 <code>__spam</code>(以至少双下划线开头,至多单下划线结尾)随即都被替代为 <code>_classname__spam</code>,去掉前导下划线的 <code>classname</code> 即当前的类名。这种混淆不关心标识符的语法位置,所以可用来定义私有类实例和类变量、方法,以及全局变量,甚至于将其它类的实例保存为私有变量。混淆名长度超过255个字符的时候可能会发生截断。在类的外部,或类名只包含下划线时,不会发生截断。<P>Name mangling is intended to give classes an easy way to define
``private'' instance variables and methods, without having to worry
about instance variables defined by derived classes, or mucking with
instance variables by code outside the class.  Note that the mangling
rules are designed mostly to avoid accidents; it still is possible for
a determined soul to access or modify a variable that is considered
private.  This can even be useful in special circumstances, such as in
the debugger, and that's one reason why this loophole is not closed.
(Buglet: derivation of a class with the same name as the base class
makes use of private variables of the base class possible.)<P>命名混淆意在给出一个在类中定义“私有”实例变量和方法的简单途径,避免派生类的实例变量定义产生问题,或者与外界代码中的变量搞混。要注意的是混淆规则主要目的在于避免意外错误,被认作为私有的变量仍然有可能被访问或修改。在特定的场合它也是有用的,比如调试的时候,这也是一直没有堵上这个漏洞的原因之一(小漏洞:派生类和基类取相同的名字就可以使用基类的私有变量。)<P>Notice that code passed to <code>exec</code>, <code>eval()</code> or
<code>evalfile()</code> does not consider the classname of the invoking
class to be the current class; this is similar to the effect of the
<code>global</code> statement, the effect of which is likewise restricted to
code that is byte-compiled together.  The same restriction applies to
<code>getattr()</code>, <code>setattr()</code> and <code>delattr()</code>, as well as
when referencing <code>__dict__</code> directly.<P>要注意的是传入 <code>exec</code>,<code>eval()</code> 或 <code>evalfile()</code> 的代码不会将调用它们的类视作当前类,这与 <code>global</code> 语句的情况类似,<code>global</code> 的作用局限于“同一批”进行字节编译的代码。同样的限制也适用于 <code>getattr()</code>,<code>setattr()</code> 和<code>delattr()</code>,以及直接引用 <code>__dict__</code> 的时候。<P><H1><A NAME="SECTION0011700000000000000000"></A><A NAME="odds"></A><BR>9.7 补充 Odds and Ends </H1><P>Sometimes it is useful to have a data type similar to the Pascal
``record'' or C ``struct'', bundling together a couple of named data
items.  An empty class definition will do nicely:<P>有时类似于Pascal中“记录(record)”或C中“结构(struct)”的数据类型很有用,它将一组已命名的数据项绑定在一起。一个空的类定义可以很好的实现这它:<P><div class="verbatim"><pre>
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</pre></div><P>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
<tt class="method">read()</tt> and <tt class="method">readline()</tt> that gets the data from a string
buffer instead, and pass it as an argument.<P>某一段 Python
代码需要一个特殊的抽象数据结构的话,通常可以传入一个类,事实上这模仿了该类的方法。例如,如果你有一个用于从文件对象中格式化数据的函数,你可以定义一个带有
<tt class="method">read()</tt> 和 <tt class="method">readline()</tt>
方法的类,以此从字符串缓冲读取数据,然后将该类的对象作为参数传入前述的函数。<P>Instance method objects have attributes, too: <code>m.im_self</code> is the
object of which the method is an instance, and <code>m.im_func</code> is the
function object corresponding to the method.<P>实例方法对象也有属性: <code>m.im_self</code> 是一个实例方法所属的对象,而 <code>m.im_func</code> 是这个方法对应的函数对象。<P><H1><A NAME="SECTION0011800000000000000000"></A><A NAME="exceptionClasses"></A><BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -