Python中的内置函数

论坛 期权论坛     
选择匿名的用户   2021-5-29 21:48   374   0
<div class="blogpost-body" id="cnblogs_post_body">
<h1>2.1 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> </p>
<p> </p>
<dl>
   
  
</dl>
<table cellpadding="0" cellspacing="0"><tbody><tr><td><b><tt class="function" id="l2h-6">__import__</tt></b>(</td><td><var>name</var><big>[</big><var>, globals</var><big>[</big><var>, locals</var><big>[</big><var>, fromlist</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></tbody></table>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="http://blog.donews.com/distiner/archive/2006/02/15/module-rexec.html">rexec</a></tt>. See also the built-in module
<tt class="module"><a href="http://blog.donews.com/distiner/archive/2006/02/15/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 &#34;<tt class="samp">import spam</tt>&#34; results in the following call: <code>__import__(&#39;spam&#39;,</code> <code>globals(),</code> <code>locals(), [])</code>; the statement &#34;<tt class="samp">from spam.ham import eggs</tt>&#34; results in &#34;<tt class="samp">__import__(&#39;spam.ham&#39;, globals(), locals(), [&#39;eggs&#39;])</tt>&#34;. Note that even though <code>locals()</code> and <code>[&#39;eggs&#39;]</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>
<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, <em>not</em> 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 &#34;<tt class="samp">import spam.ham.eggs</tt>&#34;, the top-level package <tt class="module">spam</tt> must be placed in the importing namespace, but when using &#34;<tt class="samp">from spam.ham import eggs</tt>&#34;, 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>
<p> </p>
<div class="verbatim">
  <pre class="blockcode">def my_import(name):<br>    mod &#61; __import__(name)<br>    components &#61; name.split(&#39;.&#39;)<br>    for comp in components[1:]:<br>        mod &#61; getattr(mod, comp)<br>    return mod<br></pre>
</div>
<p> </p>
<dl>
   
  
</dl>
<table cellpadding="0" cellspacing="0"><tbody><tr><td><b><tt class="function" id="l2h-8">abs</tt></b>(</td><td><var>x</var>)</td></tr></tbody></table>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.
<font size="5"><span style="color:rgb(0,0,255);font-family:Arial;">该函数返回一个number的绝对值。参数x可以是plain、long interger或者浮点。如果参数是复数,返回其数量级。</span></font>
<br>
<p> </p>
<dl>
   
  
</dl>
<table cellpadding="0" cellspacing="0"><tbody><tr><td><b><tt class="function" id="l2h-9">basestring</tt></b>(</td><td><var></var>)</td></tr></tbody></table>This abstract type is the superclass for
<tt class="class">str</tt> and
<tt class="class">unicode</tt>. It cannot be called or instantiated, but it can be used to test whether an object is an instance of
<tt class="class">str</tt> or
<tt class="class">unicode</tt>.
<code>isinstance(obj, basestring)</code> is equivalent to
<code>isinstance(obj, (str, unicode))</code>.
<span class="versionnote">New in version 2.3.</span>
<font size="5" style="color:rgb(0,0,255);"><span style="font-family:Arial;">这种抽象类型是str或者unicode类型的父类。它本身不能被调用或者实例化,但是可以用以测试一个对象到底是不是str或者unicode类型。isinstance(obj,basestring)等同isinstance(obj,(str,unicode))。</span></font>
<br>
<p> </p>
<dl>
   
  
</dl>
<table cellpadding="0" cellspacing="0"><tbody><tr><td><b><tt class="function" id="l2h-10">bool</tt></b>(</td><td><var></var><big>[</big><var>x</var><big>]</big><var></var>)</td></tr></tbody></table>Convert a value to a Boolean, using the standard truth testing procedure. If
<var>x</var> is false or omitted, this returns
<tt class="constant">False</tt>; otherwise it returns
<tt class="constant">True</tt>.
<tt class="class">bool</tt> is also a class
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP