📄 40.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> Special Methods</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="33.html" class="navtitle">3. Types and Objects</a> > <span class="nonavtitle">Special Methods</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="39.html" title="Built-in Types"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=40" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="40.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="41.html" title="Performance and Memory Considerations"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A55%3A00+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104029061153098000086086</font><a href="read3.asp?bookname=0735710910&snode=40&now=5%2F28%2F2002+8%3A55%3A00+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Special Methods</h3>
<p>All the built-in datatypes consist of some data and a collection of special object methods. The names of special methods are always preceded and followed by double underscores (<tT CLAss="monofont">_ _</tt>). These methods are automatically triggered by the interpreter as a program executes. For example, the operation <I><TT Class="monofont">x</TT></I>
<Tt class="monofont"> + </tt><i><tt class="monofont">y</tt></i>
is mapped to an internal method <i><Tt cLass="monofont">x</Tt></i>
<tt ClasS="monofont">._ _add_ _ (</TT><I><tt clASS="monofont">y</Tt></i>
<tt CLASs="monofont">)</tt>, and an indexing operation <i><TT CLass="monofont">x</tt></i>
<tt class="monofont">[</tt><i><tt clAss="monofont">k</Tt></i>
<tT clasS="monofont">]</tt> is mapped to <i><TT CLass="monofont">x</tT></I>
<TT clasS="monofont">._ _getitem_ _ (</TT><I><tt clASS="monofont">k</Tt></i>
<tt class="monofont">)</tt>. The behavior of each datatype depends entirely on the set of special methods that it implements.</p>
<p>Although it抯 not possible to alter the behavior of built-in types (or even to invoke any of their special methods directly by name, as just suggested), it抯 possible to use class definitions to define new objects that behave like the built-in types. To do this, supply implementations of the special methods described in this section.</p>
<h4>Object Creation, Destruction, and Representation</h4>
<p>The methods in <a hrEf="40#4.html">Table 3.7</a> initialize, destroy, and represent objects. The <Tt clAss="monofont">_ _init_ _()</tt> method initializes the attributes of an object and is called immediately after an object has been newly created. The <Tt clASS="monofont">_ _del_ _( )</Tt> method is invoked when an object is about to be destroyed. This method is invoked only when an object is no longer in use. It抯 important to note that the statement <tt cLASS="monofont">delx </tt><i><tT CLAss="monofont">x</tt></I>
only decrements an object抯 reference count and doesn抰 necessarily result in a call to this function.</P>
<A Name="4"></a><p><table border="1" cellSpaCing="0" CellpAddiNG="1" WIdth="100%">
<cAPTIon><h5>Table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -