Maison > Article > développement back-end > 在 Python 中,为什么 pow 这样的函数可以直接调用,而 floor 这样的函数得先导入模块?
python新手,望大神们多指教
<code class="language-python"><span class="err">$</span> <span class="n">python</span>
<span class="n">Python</span> <span class="mf">2.7</span><span class="o">.</span><span class="mi">5</span> <span class="p">(</span><span class="n">default</span><span class="p">,</span> <span class="n">Mar</span> <span class="mi">9</span> <span class="mi">2014</span><span class="p">,</span> <span class="mi">22</span><span class="p">:</span><span class="mi">15</span><span class="p">:</span><span class="mo">05</span><span class="p">)</span>
<span class="p">[</span><span class="n">GCC</span> <span class="mf">4.2</span><span class="o">.</span><span class="mi">1</span> <span class="n">Compatible</span> <span class="n">Apple</span> <span class="n">LLVM</span> <span class="mf">5.0</span> <span class="p">(</span><span class="n">clang</span><span class="o">-</span><span class="mf">500.0</span><span class="o">.</span><span class="mi">68</span><span class="p">)]</span> <span class="n">on</span> <span class="n">darwin</span>
<span class="n">Type</span> <span class="s">"help"</span><span class="p">,</span> <span class="s">"copyright"</span><span class="p">,</span> <span class="s">"credits"</span> <span class="ow">or</span> <span class="s">"license"</span> <span class="k">for</span> <span class="n">more</span> <span class="n">information</span><span class="o">.</span>
<span class="o">>>></span> <span class="mi">2</span> <span class="o">**</span> <span class="mi">5</span>
<span class="mi">32</span>
<span class="o">>>></span> <span class="nb">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="mi">32</span>
<span class="o">>>></span> <span class="n">__builtins__</span>
<span class="o"><span class="n">module</span> <span class="s">'__builtin__'</span> <span class="p">(</span><span class="n">built</span><span class="o">-</span><span class="ow">in</span><span class="p">)</span><span class="o">></span>
<span class="o">>>></span> <span class="nb">dir</span><span class="p">(</span><span class="n">__builtins__</span><span class="p">)</span>
<span class="p">[</span><span class="s">'ArithmeticError'</span><span class="p">,</span> <span class="s">'AssertionError'</span><span class="p">,</span> <span class="s">'AttributeError'</span><span class="p">,</span> <span class="s">'BaseException'</span><span class="p">,</span> <span class="s">'BufferError'</span><span class="p">,</span> <span class="s">'BytesWarning'</span><span class="p">,</span> <span class="s">'DeprecationWarning'</span><span class="p">,</span> <span class="s">'EOFError'</span><span class="p">,</span> <span class="s">'Ellipsis'</span><span class="p">,</span> <span class="s">'EnvironmentError'</span><span class="p">,</span> <span class="s">'Exception'</span><span class="p">,</span> <span class="s">'False'</span><span class="p">,</span> <span class="s">'FloatingPointError'</span><span class="p">,</span> <span class="s">'FutureWarning'</span><span class="p">,</span> <span class="s">'GeneratorExit'</span><span class="p">,</span> <span class="s">'IOError'</span><span class="p">,</span> <span class="s">'ImportError'</span><span class="p">,</span> <span class="s">'ImportWarning'</span><span class="p">,</span> <span class="s">'IndentationError'</span><span class="p">,</span> <span class="s">'IndexError'</span><span class="p">,</span> <span class="s">'KeyError'</span><span class="p">,</span> <span class="s">'KeyboardInterrupt'</span><span class="p">,</span> <span class="s">'LookupError'</span><span class="p">,</span> <span class="s">'MemoryError'</span><span class="p">,</span> <span class="s">'NameError'</span><span class="p">,</span> <span class="s">'None'</span><span class="p">,</span> <span class="s">'NotImplemented'</span><span class="p">,</span> <span class="s">'NotImplementedError'</span><span class="p">,</span> <span class="s">'OSError'</span><span class="p">,</span> <span class="s">'OverflowError'</span><span class="p">,</span> <span class="s">'PendingDeprecationWarning'</span><span class="p">,</span> <span class="s">'ReferenceError'</span><span class="p">,</span> <span class="s">'RuntimeError'</span><span class="p">,</span> <span class="s">'RuntimeWarning'</span><span class="p">,</span> <span class="s">'StandardError'</span><span class="p">,</span> <span class="s">'StopIteration'</span><span class="p">,</span> <span class="s">'SyntaxError'</span><span class="p">,</span> <span class="s">'SyntaxWarning'</span><span class="p">,</span> <span class="s">'SystemError'</span><span class="p">,</span> <span class="s">'SystemExit'</span><span class="p">,</span> <span class="s">'TabError'</span><span class="p">,</span> <span class="s">'True'</span><span class="p">,</span> <span class="s">'TypeError'</span><span class="p">,</span> <span class="s">'UnboundLocalError'</span><span class="p">,</span> <span class="s">'UnicodeDecodeError'</span><span class="p">,</span> <span class="s">'UnicodeEncodeError'</span><span class="p">,</span> <span class="s">'UnicodeError'</span><span class="p">,</span> <span class="s">'UnicodeTranslateError'</span><span class="p">,</span> <span class="s">'UnicodeWarning'</span><span class="p">,</span> <span class="s">'UserWarning'</span><span class="p">,</span> <span class="s">'ValueError'</span><span class="p">,</span> <span class="s">'Warning'</span><span class="p">,</span> <span class="s">'ZeroDivisionError'</span><span class="p">,</span> <span class="s">'_'</span><span class="p">,</span> <span class="s">'__debug__'</span><span class="p">,</span> <span class="s">'__doc__'</span><span class="p">,</span> <span class="s">'__import__'</span><span class="p">,</span> <span class="s">'__name__'</span><span class="p">,</span> <span class="s">'__package__'</span><span class="p">,</span> <span class="s">'abs'</span><span class="p">,</span> <span class="s">'all'</span><span class="p">,</span> <span class="s">'any'</span><span class="p">,</span> <span class="s">'apply'</span><span class="p">,</span> <span class="s">'basestring'</span><span class="p">,</span> <span class="s">'bin'</span><span class="p">,</span> <span class="s">'bool'</span><span class="p">,</span> <span class="s">'buffer'</span><span class="p">,</span> <span class="s">'bytearray'</span><span class="p">,</span> <span class="s">'bytes'</span><span class="p">,</span> <span class="s">'callable'</span><span class="p">,</span> <span class="s">'chr'</span><span class="p">,</span> <span class="s">'classmethod'</span><span class="p">,</span> <span class="s">'cmp'</span><span class="p">,</span> <span class="s">'coerce'</span><span class="p">,</span> <span class="s">'compile'</span><span class="p">,</span> <span class="s">'complex'</span><span class="p">,</span> <span class="s">'copyright'</span><span class="p">,</span> <span class="s">'credits'</span><span class="p">,</span> <span class="s">'delattr'</span><span class="p">,</span> <span class="s">'dict'</span><span class="p">,</span> <span class="s">'dir'</span><span class="p">,</span> <span class="s">'divmod'</span><span class="p">,</span> <span class="s">'enumerate'</span><span class="p">,</span> <span class="s">'eval'</span><span class="p">,</span> <span class="s">'execfile'</span><span class="p">,</span> <span class="s">'exit'</span><span class="p">,</span> <span class="s">'file'</span><span class="p">,</span> <span class="s">'filter'</span><span class="p">,</span> <span class="s">'float'</span><span class="p">,</span> <span class="s">'format'</span><span class="p">,</span> <span class="s">'frozenset'</span><span class="p">,</span> <span class="s">'getattr'</span><span class="p">,</span> <span class="s">'globals'</span><span class="p">,</span> <span class="s">'hasattr'</span><span class="p">,</span> <span class="s">'hash'</span><span class="p">,</span> <span class="s">'help'</span><span class="p">,</span> <span class="s">'hex'</span><span class="p">,</span> <span class="s">'id'</span><span class="p">,</span> <span class="s">'input'</span><span class="p">,</span> <span class="s">'int'</span><span class="p">,</span> <span class="s">'intern'</span><span class="p">,</span> <span class="s">'isinstance'</span><span class="p">,</span> <span class="s">'issubclass'</span><span class="p">,</span> <span class="s">'iter'</span><span class="p">,</span> <span class="s">'len'</span><span class="p">,</span> <span class="s">'license'</span><span class="p">,</span> <span class="s">'list'</span><span class="p">,</span> <span class="s">'locals'</span><span class="p">,</span> <span class="s">'long'</span><span class="p">,</span> <span class="s">'map'</span><span class="p">,</span> <span class="s">'max'</span><span class="p">,</span> <span class="s">'memoryview'</span><span class="p">,</span> <span class="s">'min'</span><span class="p">,</span> <span class="s">'next'</span><span class="p">,</span> <span class="s">'object'</span><span class="p">,</span> <span class="s">'oct'</span><span class="p">,</span> <span class="s">'open'</span><span class="p">,</span> <span class="s">'ord'</span><span class="p">,</span> <span class="s">'pow'</span><span class="p">,</span> <span class="s">'print'</span><span class="p">,</span> <span class="s">'property'</span><span class="p">,</span> <span class="s">'quit'</span><span class="p">,</span> <span class="s">'range'</span><span class="p">,</span> <span class="s">'raw_input'</span><span class="p">,</span> <span class="s">'reduce'</span><span class="p">,</span> <span class="s">'reload'</span><span class="p">,</span> <span class="s">'repr'</span><span class="p">,</span> <span class="s">'reversed'</span><span class="p">,</span> <span class="s">'round'</span><span class="p">,</span> <span class="s">'set'</span><span class="p">,</span> <span class="s">'setattr'</span><span class="p">,</span> <span class="s">'slice'</span><span class="p">,</span> <span class="s">'sorted'</span><span class="p">,</span> <span class="s">'staticmethod'</span><span class="p">,</span> <span class="s">'str'</span><span class="p">,</span> <span class="s">'sum'</span><span class="p">,</span> <span class="s">'super'</span><span class="p">,</span> <span class="s">'tuple'</span><span class="p">,</span> <span class="s">'type'</span><span class="p">,</span> <span class="s">'unichr'</span><span class="p">,</span> <span class="s">'unicode'</span><span class="p">,</span> <span class="s">'vars'</span><span class="p">,</span> <span class="s">'xrange'</span><span class="p">,</span> <span class="s">'zip'</span><span class="p">]</span>
<span class="o">>>></span> <span class="nb">pow</span>
<span class="o"><span class="n">built</span><span class="o">-</span><span class="ow">in</span> <span class="n">function</span> <span class="nb">pow</span><span class="o">></span>
<span class="o">>>></span> <span class="n">mypow</span> <span class="o">=</span> <span class="nb">pow</span>
<span class="o">>>></span> <span class="n">mypow</span>
<span class="o"><span class="n">built</span><span class="o">-</span><span class="ow">in</span> <span class="n">function</span> <span class="nb">pow</span><span class="o">></span>
<span class="o">>>></span> <span class="n">__builtins__</span><span class="o">.</span><span class="n">pow</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">mypow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="o">>>></span> <span class="nb">pow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="mi">31</span>
<span class="o">>>></span> <span class="mi">2</span> <span class="o">**</span> <span class="mi">5</span>
<span class="mi">32</span>
</span></span></span></code>
看到蓝色来冒充Python专家了,特来批判一番。这里首先是黑魔法,不然有些问题就解释不清楚啦。<code class="language-text">>>> __builtins__
<module>
>>> pow(2,2)
4
>>> __builtins__ = None
>>> pow(2,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pow' is not defined
>>> __builtins__ = {'pow':1}
>>> pow
1
>>>
</module></stdin></module></code>
很多人扯一大堆道理,然而对于本问题并没什么卵用。<code class="language-c"><span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">builtin_methods</span><span class="p">[]</span>
</code>
pow 貌似比 floor 更常用 更适合作为 build-in 存在
这个问题的实质就是:为什么有的函数在__builtins__,而有的不在?
除了scope原因之外,造成這樣scope的原因,還有一個可能是Pow 是int, floor可能是float。雖然python裡面並沒有什麼type。