📄 built-in-funcs.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>2.3 Built-in Functions </title>
<META NAME="description" CONTENT="2.3 Built-in Functions ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="previous" href="module-exceptions.html" tppabs="http://www.python.org/doc/current/lib/module-exceptions.html">
<LINK REL="up" href="builtin.html" tppabs="http://www.python.org/doc/current/lib/builtin.html">
<LINK REL="next" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-exceptions.html" tppabs="http://www.python.org/doc/current/lib/module-exceptions.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="builtin.html" tppabs="http://www.python.org/doc/current/lib/builtin.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-exceptions.html" tppabs="http://www.python.org/doc/current/lib/module-exceptions.html">2.2 Built-in Exceptions</A>
<b class="navlabel">Up:</b> <a class="sectref" href="builtin.html" tppabs="http://www.python.org/doc/current/lib/builtin.html">2. Built-in Types, Exceptions</A>
<b class="navlabel">Next:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1>
<BR>
2.3 Built-in Functions
</H1>
<P>
The Python interpreter has a number of functions built into it that
are always available. They are listed here in alphabetical order.
<P>
<P>
<dl><dt><b><a name='l2h-143'><tt class='function'>__import__</tt></a></b> (<var>name</var><big>[</big><var>, globals</var><big>[</big><var>, locals</var><big>[</big><var>, fromlist</var><big>]</big><big>]</big><big>]</big>)
<dd>
This function is invoked by the
<tt class="keyword">import</tt> statement. It mainly
exists so that you can replace it with another function that has a
compatible interface, in order to change the semantics of the
<tt class="keyword">import</tt> statement. For examples of why and how you would do
this, see the standard library modules
<tt class="module">ihooks</tt> and
<tt class='module'><a href="module-rexec.html" tppabs="http://www.python.org/doc/current/lib/module-rexec.html">rexec</a></tt>. See also the built-in module
<tt class='module'><a href="module-imp.html" tppabs="http://www.python.org/doc/current/lib/module-imp.html">imp</a></tt>, which defines some useful
operations out of which you can build your own
<tt class="function">__import__()</tt> function.
<P>
For example, the statement `<code>import</code> <code>spam</code>' results in the
following call:
<code>__import__('spam',</code> <code>globals(),</code> <code>locals(), [])</code>;
the statement <code>from</code> <code>spam.ham import</code> <code>eggs</code> results
in <code>__import__('spam.ham',</code> <code>globals(),</code> <code>locals(),</code>
<code>['eggs'])</code>.
Note that even though <code>locals()</code> and <code>['eggs']</code> are passed
in as arguments, the <tt class="function">__import__()</tt> function does not set the
local variable named <code>eggs</code>; this is done by subsequent code that
is generated for the import statement. (In fact, the standard
implementation does not use its <var>locals</var> argument at all, and uses
its <var>globals</var> only to determine the package context of the
<tt class="keyword">import</tt> statement.)
<P>
When the <var>name</var> variable is of the form <code>package.module</code>,
normally, the top-level package (the name up till the first dot) is
returned, <i>not</i> the module named by <var>name</var>. However, when a
non-empty <var>fromlist</var> argument is given, the module named by
<var>name</var> is returned. This is done for compatibility with the
bytecode generated for the different kinds of import statement; when
using "<tt class="samp">import spam.ham.eggs</tt>", the top-level package <code>spam</code>
must be placed in the importing namespace, but when using "<tt class="samp">from
spam.ham import eggs</tt>", the <code>spam.ham</code> subpackage must be used to
find the <code>eggs</code> variable.
As a workaround for this behavior, use <tt class="function">getattr()</tt> to extract
the desired components. For example, you could define the following
helper:
<P>
<dl><dd><pre class="verbatim">
import string
def my_import(name):
mod = __import__(name)
components = string.split(name, '.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
</pre></dl>
<P>
</dl>
<P>
<dl><dt><b><a name='l2h-144'><tt class='function'>abs</tt></a></b> (<var>x</var>)
<dd>
Return the absolute value of a number. The argument may be a plain
or long integer or a floating point number. If the argument is a
complex number, its magnitude is returned.
</dl>
<P>
<dl><dt><b><a name='l2h-145'><tt class='function'>apply</tt></a></b> (<var>function, args</var><big>[</big><var>, keywords</var><big>]</big>)
<dd>
The <var>function</var> argument must be a callable object (a user-defined or
built-in function or method, or a class object) and the <var>args</var>
argument must be a sequence (if it is not a tuple, the sequence is
first converted to a tuple). The <var>function</var> is called with
<var>args</var> as the argument list; the number of arguments is the the length
of the tuple. (This is different from just calling
<code><var>func</var>(<var>args</var>)</code>, since in that case there is always
exactly one argument.)
If the optional <var>keywords</var> argument is present, it must be a
dictionary whose keys are strings. It specifies keyword arguments to
be added to the end of the the argument list.
</dl>
<P>
<dl><dt><b><a name='l2h-146'><tt class='function'>buffer</tt></a></b> (<var>object</var><big>[</big><var>, offset</var><big>[</big><var>, size</var><big>]</big><big>]</big>)
<dd>
The <var>object</var> argument must be an object that supports the
buffer call interface (such as strings, arrays, and buffers). A new
buffer object will be created which references the <var>object</var> argument.
The buffer object will be a slice from the beginning of <var>object</var>
(or from the specified <var>offset</var>). The slice will extend to the
end of <var>object</var> (or will have a length given by the <var>size</var>
argument).
</dl>
<P>
<dl><dt><b><a name='l2h-147'><tt class='function'>callable</tt></a></b> (<var>object</var>)
<dd>
Return true if the <var>object</var> argument appears callable, false if
not. If this returns true, it is still possible that a call fails,
but if it is false, calling <var>object</var> will never succeed. Note
that classes are callable (calling a class returns a new instance);
class instances are callable if they have a <tt class="method">__call__()</tt> method.
</dl>
<P>
<dl><dt><b><a name='l2h-148'><tt class='function'>chr</tt></a></b> (<var>i</var>)
<dd>
Return a string of one character whose ASCII code is the integer
<var>i</var>, e.g., <code>chr(97)</code> returns the string <code>'a'</code>. This is the
inverse of <tt class="function">ord()</tt>. The argument must be in the range [0..255],
inclusive; <tt class="exception">ValueError</tt> will be raised if <var>i</var> is
outside that range.
</dl>
<P>
<dl><dt><b><a name='l2h-149'><tt class='function'>cmp</tt></a></b> (<var>x, y</var>)
<dd>
Compare the two objects <var>x</var> and <var>y</var> and return an integer
according to the outcome. The return value is negative if <code><var>x</var>
< <var>y</var></code>, zero if <code><var>x</var> == <var>y</var></code> and strictly positive if
<code><var>x</var> > <var>y</var></code>.
</dl>
<P>
<dl><dt><b><a name='l2h-150'><tt class='function'>coerce</tt></a></b> (<var>x, y</var>)
<dd>
Return a tuple consisting of the two numeric arguments converted to
a common type, using the same rules as used by arithmetic
operations.
</dl>
<P>
<dl><dt><b><a name='l2h-151'><tt class='function'>compile</tt></a></b> (<var>string, filename, kind</var>)
<dd>
Compile the <var>string</var> into a code object. Code objects can be
executed by an <tt class="keyword">exec</tt> statement or evaluated by a call to
<tt class="function">eval()</tt>. The <var>filename</var> argument should
give the file from which the code was read; pass e.g. <code>'<string>'</code>
if it wasn't read from a file. The <var>kind</var> argument specifies
what kind of code must be compiled; it can be <code>'exec'</code> if
<var>string</var> consists of a sequence of statements, <code>'eval'</code>
if it consists of a single expression, or <code>'single'</code> if
it consists of a single interactive statement (in the latter case,
expression statements that evaluate to something else than
<code>None</code> will printed).
</dl>
<P>
<dl><dt><b><a name='l2h-152'><tt class='function'>complex</tt></a></b> (<var>real</var><big>[</big><var>, imag</var><big>]</big>)
<dd>
Create a complex number with the value <var>real</var> + <var>imag</var>*j or
convert a string or number to a complex number.
Each argument may be any numeric type (including complex).
If <var>imag</var> is omitted, it defaults to zero and the function
serves as a numeric conversion function like <tt class="function">int()</tt>,
<tt class="function">long()</tt> and <tt class="function">float()</tt>; in this case it also
accepts a string argument which should be a valid complex number.
</dl>
<P>
<dl><dt><b><a name='l2h-153'><tt class='function'>delattr</tt></a></b> (<var>object, name</var>)
<dd>
This is a relative of <tt class="function">setattr()</tt>. The arguments are an
object and a string. The string must be the name
of one of the object's attributes. The function deletes
the named attribute, provided the object allows it. For example,
<code>delattr(<var>x</var>, '<var>foobar</var>')</code> is equivalent to
<code>del <var>x</var>.<var>foobar</var></code>.
</dl>
<P>
<dl><dt><b><a name='l2h-154'><tt class='function'>dir</tt></a></b> (<big>[</big><var>object</var><big>]</big>)
<dd>
Without arguments, return the list of names in the current local
symbol table. With an argument, attempts to return a list of valid
attribute for that object. This information is gleaned from the
object's <tt class="member">__dict__</tt>, <tt class="member">__methods__</tt> and <tt class="member">__members__</tt>
attributes, if defined. The list is not necessarily complete; e.g.,
for classes, attributes defined in base classes are not included,
and for class instances, methods are not included.
The resulting list is sorted alphabetically. For example:
<P>
<dl><dd><pre class="verbatim">
>>> import sys
>>> dir()
['sys']
>>> dir(sys)
['argv', 'exit', 'modules', 'path', 'stderr', 'stdin', 'stdout']
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -