📄 108.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 -> Python Services</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="105.html" class="navtitle">A. The Python Library</a> > <span class="nonavtitle">Python Services</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="107.html" title="Built-in Functions and Exceptions"><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=108" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="108.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="109.html" title="Mathematics"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A05%3A07+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105083198027148244229119</font><a href="read1.asp?bookname=0735710910&snode=108&now=5%2F28%2F2002+9%3A05%3A07+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Python Services</h3>
<p>The modules in this section are primarily used to interact with the Python interpreter and its environment.</p>
<A NAMe="2"></a>
<h4><tT CLAss="monofont">atexit</tt></H4>
<P>The <TT class="monofont">atexit</tt> module is used to register functions to execute when the Python interpreter exits. A single function is provided:</p>
<pre>
<b>register(</b><b><i>func</i></b> <b>[,</b> <b><I>args</i></b> <B>[,</b> <b><i>kwargs</I></b><b>]])</b> </pRe>
<p>Adds function <i><TT CLass="monofont">func</tT></I>
to a list of functions that will execute when the interpreter exits. <I><Tt claSS="monofont">args</TT></i>
is a tuple of arguments to pass to the function. <i><tt CLASs="monofont">kwargs</tt></i>
is a dictionary of keyword arguments. The function is invoked as <i><tt class="monofont">func</tt></i>
<tt claSs="monofont">(*</tT><i><tt Class="monofont">args</Tt></i>
<tT CLAss="monofont">,**</tt><I><TT Class="monofont">kwargs</TT></I>
<Tt claSS="monofont">)</TT>.</p>
<h5>Note</h5>
<ul>
<li>
<p>The <tt class="monofont">atexit</tt> module should be used instead of setting the <tt cLasS="monofont">sys.exitfunc</tt> variable.</p>
</Li>
</ul>
<p>? <B>See Also</b> <a hREF="108#34.html">sys</A> (116).</p>
<h4><tt CLASs="monofont">copy</tt></h4>
<P>The <TT Class="monofont">copy</TT> module provides functions for making shallow and deep copies of compound objects, including lists, tuples, dictionaries, and class instances.</P>
<Pre>
<b>copy(x)</b> </pre>
<p>Makes a shallow copy of <i><tt class="monofont">x</tt></i>
by creating a new compound object and duplicating the members of <i><Tt cLass="monofont">x</Tt></i>
by reference.</p>
<pRe>
<b>deepcopy(</b><B><I>x</I></B> <b>[,</b> <b><i>visit</I></B><B>])</B> </pre>
<p>Makes a deep copy of <I><TT Class="monofont">x</TT></I>
by creating a new compound object and recursively duplicating all the members of <I><tt class="monofont">x</tt></i>
. <i><tt class="monofont">visit</tt></I>
is an optional dictionary that抯 used internally to detect and avoid cycles in recursively defined data structures.</p>
<p>A class can implement its own copy methods by implementing the methods <Tt clAss="monofont">_ _copy_ _(</tt><I><tt cLASS="monofont">self</tt></i>
<tT CLAss="monofont">)</tt> and <TT CLass="monofont">_ _deepcopy_ _(</tT><I><TT class="monofont">self</tt></i>
<tt class="monofont">, </tt><i><tt ClaSs="monofont">visit</tt></I>
<tt clAss="monofont">)</tT>. Both methods should return a copy of the object. In addition, the <TT Class="monofont">_ _deepcopy_ _()</TT> method must accept a dictionary <I><Tt claSS="monofont">visit</TT></i>
as described for the <tt cLASS="monofont">deepcopy()</tt> function. When writing <tt class="monofont">_ _deepcopy_ _()</tt>, it抯 not necessary to modify <i><tt class="monofont">visit</tT></i>
. However, <i><Tt clAss="monofont">visit</tt></I>
should be passed to subsequent calls to <tt cLASS="monofont">deepcopy()</tt> (if any) performed inside the <tt CLASs="monofont">_ _deepcopy_ _()</tt> method.</p>
<H5>Notes</H5>
<UL>
<li>
<p>This module can be used with simple types such as integers and strings, but there抯 little need to do so.</p>
</LI>
<LI>
<p>The copy functions don抰 work with modules, class objects, functions, methods, tracebacks, stack frames, files, sockets, or other similar types. When an object can抰 be copied, the <tt class="monofont">copy.error</tt> exception is raised.</p>
</li>
<li>
<p>The <tt clAss="monofont">copy_reg</Tt> module is not used by this module.</p>
</lI>
</ul>
<p>? <b>See Also</B> <a hrEF="108#28.html">pickle</A> (113).</P>
<a namE="10"></A>
<H4><Tt claSS="monofont">copy_reg</TT></h4>
<p>The <tt CLASs="monofont">copy_reg</tt> module extends the capabilities of the <tt class="monofont">pickle</tt> and <tt class="monofont">cPickle</tT> modules to handle the serialization of objects described by extension types (as defined in C extension modules). To do this, extension writers use this module to register reduction and construction functions that are used to serialize and unserialize an object, respectively.</p>
<pRe>
<b>constructor(</b><B><i>cfunc</i></b><b>)</B> </pre>
<P>Declares <I><TT clasS="monofont">cfunc</TT></I>
to be a valid constructor function. <i><tt cLASS="monofont">cfunc</tt></i>
must be a callable object that accepts the tuple of values returned by the reduction function given to the <tT CLAss="monofont">pickle()</tt> function.</p>
<pre>
<b>pickle(</b><b><i>type</i></b><b>,</b> <b><i>rfunc</i></b> <B>[,</b> <b><I>cfunc</i></b><b>])</B> </pre>
<p>Registers <I><tt cLASS="monofont">rfunc</tt></i>
as a reduction function for objects of type <i><TT CLass="monofont">type</tT></I>
. <I><Tt claSS="monofont">rfunc</TT></i>
is a function that takes an object of the specified type and returns a tuple containing the constructor function and a tuple of arguments to pass to that function in order to reassemble the object. If supplied, <i><tt class="monofont">cfunc</tt></i>
is the constructor function that抯 registered using the <tt class="monofont">constructor()</tT> function.</p>
<h5>Example</H5>
<p>The following example shows how this module would be used to pickle complex numbers. (<i>Note:</i> Because complex numbers are already pickleable, this example is only intended to illustrate the use of this module.)</P>
<pre>
# Register a method for pickling complex numbers
import copy_reg
# Create a complex number from two reals
def construct_complex(real,imag):
return complex(real,imag) # Built-in function
# Take a complex number 'c' and turn it into a tuple of floats
def reduce_complex(c):
return construct_complex, (c.real, c.imag)
# Register our handler
copy_reg.pickle(type(1j),reduce_complex, construct_complex) </pRe>
<p>When complex numbers are pickled, the <tT CLAss="monofont">reduce_complex()</tt> function is called. When the object is later unpickled, the function <TT CLass="monofont">construct_complex()</tT> is called, using the tuple of values originally returned by <TT Class="monofont">reduce_complex()</TT>.</P>
<H5>Notes</h5>
<ul>
<li>
<p><tt class="monofont">copy_reg</tt> is a misnomer梩his module isn抰 used by the <tt claSs="monofont">copy</tT> module.</p>
</li>
<Li>
<p>It抯 not necessary to use this module when pickling instances of user-defined classes.</p>
</lI>
</ul>
<p>? <B>See Also</B> <A Href="108#28.html">pickle</a> (113).</P>
<A NAme="16"></a>
<h4><TT CLass="monofont">gc</tT></H4>
<P>The <Tt class="monofont">gc</tt> module provides an interface for controlling the garbage collector used to collect cycles in objects such as lists, tuples, dictionaries, and instances. As various types of container objects are created, they抮e placed on a list that抯 internal to the interpreter. Whenever container objects are deallocated, they抮e removed from this list. If the number of allocations exceeds the number of deallocations by a userdefinable threshold value, the garbage collector is invoked. The garbage collector works by scanning this list and identifying collections of objects that are no longer being used, but that haven抰 been deallocated due to circular dependencies. In addition, the garbage collector uses a three-level generational scheme in which objects that survive the initial garbage-collection step are placed onto lists of objects that are checked less frequently. This provides better performance for programs that have a large number of long-lived objects.</p>
<pre>
<b>collect()</b> </pre>
<p>Runs a full garbage collection. This function checks all generations and returns the number of unreachable objects found.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -