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

📄 node11.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN"><html><head><link rel="STYLESHEET" href="style.css" type='text/css' /><link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /><link rel='start' href='../index.html' title='Python Documentation Index' /><link rel="first" href="tut.html" title='Python Tutorial' /><link rel='contents' href='node2.html' title="Contents" /><link rel='last' href='about.html' title='About this document...' /><link rel='help' href='about.html' title='About this document...' /><link rel="next" href="node12.html" /><link rel="prev" href="node10.html" /><link rel="parent" href="tut.html" /><link rel="next" href="node12.html" /><meta name='aesop' content='information' /><title>9. 类 Classes </title></head><body><DIV CLASS="navigation"><div id='top-navigation-panel' xml:id='top-navigation-panel'><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="8. 错误和异常 Errors and"  href="node10.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="10. 标准库概览 Brief Tour"  href="node12.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node10.html">8. 错误和异常 Errors and</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node12.html">10. 标准库概览 Brief Tour</A></div><hr /></div></DIV><!--End of Navigation Panel--><div class='online-navigation'><!--Table of Child-Links--><A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a><UL CLASS="ChildLinks"><LI><A href="node11.html#SECTION0011100000000000000000">9.1 有关术语的话题 A Word About Terminology</a><LI><A href="node11.html#SECTION0011200000000000000000">9.2 Python 作用域和命名空间 Python Scopes and Name Spaces</a><LI><A href="node11.html#SECTION0011300000000000000000">9.3 初识类 A First Look at Classes</a><UL><LI><A href="node11.html#SECTION0011310000000000000000">9.3.1 类定义语法 Class Definition Syntax</a><LI><A href="node11.html#SECTION0011320000000000000000">9.3.2 类对象 Class Objects</a><LI><A href="node11.html#SECTION0011330000000000000000">9.3.3 实例对象 Instance Objects</a><LI><A href="node11.html#SECTION0011340000000000000000">9.3.4 方法对象 Method Objects</a></ul><LI><A href="node11.html#SECTION0011400000000000000000">9.4 一些说明 Random Remarks</a><LI><A href="node11.html#SECTION0011500000000000000000">9.5 继承 Inheritance</a><UL><LI><A href="node11.html#SECTION0011510000000000000000">9.5.1 多继承 Multiple Inheritance</a></ul><LI><A href="node11.html#SECTION0011600000000000000000">9.6 私有变量 Private Variables</a><LI><A href="node11.html#SECTION0011700000000000000000">9.7 补充 Odds and Ends</a><LI><A href="node11.html#SECTION0011800000000000000000">9.8 异常也是类 Exceptions Are Classes Too</a><LI><A href="node11.html#SECTION0011900000000000000000">9.9 迭代器 Iterators</a><LI><A href="node11.html#SECTION00111000000000000000000">9.10 生成器 Generators</a></ul><!--End of Table of Child-Links--></div><HR><H1><A NAME="SECTION0011000000000000000000"></A><A NAME="classes"></A><BR>9. 类 Classes </H1><P>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, a method can call the method of a base class with the
same name.  Objects can contain an arbitrary amount of private data.<P>Python 在尽可能不增加新的语法和语义的情况下加入了类机制。这种机制是 C++ 和 Modula-3 的混合。Python中的类没有在用户和定义之间建立一个绝对的屏障,而是依赖于用户自觉的不去“破坏定义”。然而,类机制最重要的功能都完整的保留下来。类继承机制允许多继承,派生类可以覆盖(override)基类中的任何方法,方法中可以调用基类中的同名方法。对象可以包含任意数量的私有成员。<P>In C++ terminology, all class members (including the data members) are
<em>public</em>, and all member functions are <em>virtual</em>.  There are
no special constructors or destructors.  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, albeit in the wider
sense of the word: in Python, all data types 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++ but unlike in Modula-3, most
built-in operators with special syntax (arithmetic operators,
subscripting etc.) can be redefined for class instances.<P>用 C++ 术语来讲,所有的类成员(包括数据成员)都是公有( <em>public</em> )的,所有的成员函数都是虚拟( <em>virtual</em> )的。没有特定的构造和析构函数。用Modula-3的术语来讲,在成员方法中没有什么简便的方式(shorthands)可以引用对象的成员:方法函数在定义时需要以引用的对象做为第一个参数,调用时则会隐式引用对象。这样就形成了语义上的引入和重命名。( This provides semantics for importing and renaming. )但是,像 C++ 而非 Modula-3 中那样,大多数带有特殊语法的内置操作符(算法运算符、下标等)都可以针对类的需要重新定义。<P><H1><A NAME="SECTION0011100000000000000000"></A><A NAME="terminology"></A><BR>9.1 有关术语的话题 A Word About Terminology </H1><P>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.)<P>由于没有什么关于类的通用术语,我从 Smalltalk 和 C++ 中借用一些(我更希望用 Modula-3 的,因为它的面向对象机制比 C++更接近Python,不过我想没多少读者听说过它)。<P>I also have to warn you that there's a terminological pitfall for
object-oriented readers: the word ``object'' in Python does not
necessarily mean a class instance.  Like C++ and Modula-3, and
unlike Smalltalk, not all types in Python are classes: the basic
built-in types like integers and lists are not, and even somewhat more
exotic types like files aren't.  However, <em>all</em> Python types
share a little bit of common semantics that is best described by using
the word object.<P>我要提醒读者,这里有一个面向对象方面的术语陷阱,在 Python 中“对象”这个词不一定指类实例。Python 中并非所有的类型都是类:例如整型、链表这些内置数据类型就不是,甚至某些像文件这样的外部类型也不是,这一点类似于 C++ 和 Modula-3,而不像 Smalltalk。然而,所有的 Python 类型在语义上都有一点相同之处:描述它们的最贴切词语是“对象”。<P>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 an
(intended!) effect on the semantics of Python code involving mutable
objects such as lists, dictionaries, and most types representing
entities outside the program (files, windows, etc.).  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.<P>对象是被特化的,多个名字(在多个作用域中)可以绑定同一个对象。这相当于其它语言中的别名。通常对 Python 的第一印象中会忽略这一点,使用那些不可变的基本类型(数值、字符串、元组)时也可以很放心的忽视它。然而,在 Python 代码调用字典、链表之类可变对象,以及大多数涉及程序外部实体(文件、窗体等等)的类型时,这一语义就会有影响。这通用有助于优化程序,因为别名的行为在某些方面类似于指针。例如,很容易传递一个对象,因为在行为上只是传递了一个指针。如果函数修改了一个通过参数传递的对象,调用者可以接收到变化--在 Pascal 中这需要两个不同的参数传递机制。<P><H1><A NAME="SECTION0011200000000000000000"></A><A NAME="scopes"></A><BR>9.2 Python 作用域和命名空间 Python Scopes and Name Spaces </H1><P>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.<P>在介绍类之前,我首先介绍一些有关 Python 作用域的规则:类的定义非常巧妙的运用了命名空间,要完全理解接下来的知识,需要先理解作用域和命名空间的工作原理。另外,这一切的知识对于任何高级 Python 程序员都非常有用。<P>Let's begin with some definitions.<P>我们从一些定义开始。<P>A <em>namespace</em> 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 <tt class="function">abs()</tt>, 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.<P><em>命名空间</em>是从命名到对象的映射。当前<em>命名空间</em>主要是通过 Python 字典实现的,不过通常不关心具体的实现方式(除非出于性能考虑),以后也有可能会改变其实现方式。以下有一些命名空间的例子:内置命名(像 <tt class="function"><</tt>#2558#>abs() 这样的函数,以及内置异常名)集,模块中的全局命名,函数调用中的局部命名。某种意义上讲对象的属性集也是一个<em>命名空间</em>。关于<em>命名空间</em>需要了解的一件很重要的事就是不同<em>命名空间</em>中的命名没有任何联系,例如两个不同的模块可能都会定义一个名为“maximize”的函数而不会发生混淆--用户必须以模块名为前缀来引用它们。<P>By the way, I use the word <em>attribute</em> for any name following a
dot -- for example, in the expression <code>z.real</code>, <code>real</code> is
an attribute of the object <code>z</code>.  Strictly speaking, references to
names in modules are attribute references: in the expression
<code>modname.funcname</code>, <code>modname</code> is a module object and
<code>funcname</code> 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!
<A NAME="tex2html9"  HREF="#foot2455"><SUP>9.1</SUP></A><P>顺便提一句,我称 Python 中任何一个“.”之后的命名为<em>属性</em>--例如,表达式 <code>z.real</code> 中的 <code>real</code> 是对象 <code>z</code> 的一个属性。严格来讲,从模块中引用命名是引用属性:表达式 <code>modname.funcname</code> 中, <code>modname</code> 是一个模块对象,<code>funcname</code> 是它的一个属性。因此,模块的属性和模块中的全局命名有直接的映射关系:它们共享同一命名空间!<A NAME="tex2html10"  HREF="#foot1679"><SUP>9.2</SUP></A><P>Attributes may be read-only or writable.  In the latter case,
assignment to attributes is possible.  Module attributes are writable:
you can write "<tt class="samp">modname.the_answer = 42</tt>".  Writable attributes may
also be deleted with the <tt class="keyword">del</tt> statement.  For example,
"<tt class="samp">del modname.the_answer</tt>" will remove the attribute
<tt class="member">the_answer</tt> from the object named by <code>modname</code>.<P>属性可以是只读过或写的。后一种情况下,可以对属性赋值。你可以这样作:
"<tt class="samp">modname.the_answer = 42</tt>"。可写的属性也可以用 <tt class="keyword">del</tt>
语句删除。例如:"<tt class="samp">del modname.the_answer</tt>" 会从
 <code>modname</code> 对象中删除 <tt class="member">the_answer</tt> 属性。<P>Name spaces 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
<tt class="module">__main__</tt>, so they have their own global namespace.  (The
built-in names actually also live in a module; this is called
<tt class="module">__builtin__</tt>.)<P>不同的命名空间在不同的时刻创建,有不同的生存期。包含内置命名的命名空间在 Python 解释器启动时创建,会一直保留,不被删除。模块的全局命名空间在模块定义被读入时创建,通常,模块命名空间也会一直保存到解释器退出。由解释器在最高层调用执行的语句,不管它是从脚本文件中读入还是来自交互式输入,都是 __main__ 模块的一部分,所以它们也拥有自己的命名空间。(内置命名也同样被包含在一个模块中,它被称作 __builtin__ 。)<P>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.<P>当函数被调用时创建一个局部命名空间,函数反正返回过抛出一个未在函数内处理的异常时删除。(实际上,说是遗忘更为贴切)。当然,每一个递归调用拥有自己的命名空间。

⌨️ 快捷键说明

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