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

📄 132.html

📁 国外python经典教材,python爱好者的首选
💻 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 -&gt; Defining New Types</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> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="121.html" class="navtitle">B. Extending and Embedding Python</a> &gt; <span class="nonavtitle">Defining New Types</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="131.html" title="Low-Level Functions on Built-in Types"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=132" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="132.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="133.html" title="Special Methods for Types"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A10%3A26+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161104058145056204253196054</font><a href="read4.asp?bookname=0735710910&snode=132&now=5%2F28%2F2002+9%3A10%3A26+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Defining New Types</h3>
<p>New types of objects can also be defined in extension modules. However, this process is considerably more advanced than simply accessing a few C functions. Because of this complexity, you should consider implementing a new type only in the following situations:</p>

<UL>
<LI>
<p>The type is not easily constructed from existing Python types.</p>
</li>
<LI>
<P>The type requires interaction with the operating system or another special feature not provided by the interpreter or the standard library.</P>
</li>
<li>
<P>The type hasn抰 already been implemented elsewhere. For example, efficient matrix types have already been implemented, so it would make little sense to reinvent them. It抯 always a good idea to check the Python libraries and newsgroups before implementing a new type. The Vaults of Parnassas Python Resources (<A TArget="_blank" href="http://www.vex.net/parnassus">http://www.vex.net/parnassus</a>) is also an excellent resource for finding previously implemented extension modules.</p>
</li>
</ul>
<p>The process of creating a new Python type involves the following steps:</p>

<a namE="app02pro01"></a>
<p>
<P>
<p>
<p>
<p>
<P>
<p>
<p>
<p><oL>
<li vALUE="1">

<p>Define a data structure that contains the actual data stored in the type梖or example, the <tt cLASS="monofont">List</tt>  type has an array of elements containing the list items.</p>
</lI><LI Value="2">

<P>Define the functions that are going to serve as methods of the type梖or example, the <TT Class="monofont">append()</tt>  method of a <tt class="monofont">List</tt> object.</p>
</li><li ValUe="3">

<p>Define a pair of functions for creating and destroying the type.</p>
</Li><li vAlue="4">

<P>Define a set of functions that implement the special methods such as <TT Class="monofont">_ _add_ _()</TT> and <TT clasS="monofont">_ _getitem_ _()</TT> that are supported by the type, as described in <A href="33.html">Chapter 3</A>, 揟ypes and Objects.

⌨️ 快捷键说明

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