ホームページ  >  記事  >  バックエンド開発  >  在 Python 中,为什么 pow 这样的函数可以直接调用,而 floor 这样的函数得先导入模块?

在 Python 中,为什么 pow 这样的函数可以直接调用,而 floor 这样的函数得先导入模块?

WBOY
WBOYオリジナル
2016-06-06 16:23:121685ブラウズ

python新手,望大神们多指教

回复内容:

本来写在 @bhuztez 大大的回答的评论里的。算了还是发到顶层好了。

诶这里 @蓝色 大大的回答真的误解一些了, @bhuztez 大大的回答才是完全正解。

这个问题,要看最精辟的答案的请看 @flow memory 大大的,要深入到Python的内部机制的请看 @bhuztez 大大的,要看点具体代码的请看 @蓝色 大大修正过的答案。

Python的__builtin___模块完全是个运行时的东西, @蓝色 大大引用的代码其实是在VM初始化的时候把初始的__builtin___模块中的名字与函数指针的对应关系注册好;然而Python的(源码到字节码)编译器是不关心这个的。
Python的pow()跟像GCC的__builtin_powi()不可以相提并论;前者的行为可以在运行时改变,而编译器完全不把它当作特殊的东西看待;后者则是编译器直接支持的intrinsic function。

刚初始化好的时候,__builtin__模块里的"pow"映射到的是builtin_pow()函数,后者进一步调用PyNumber_Power()函数来实现功能;cpython/bltinmodule.c at 2.7 · python/cpython · GitHub
Python的源码编译器会把 ** 运算符编译为BINARY_POWER字节码指令,而Python的字节码解释器为BINARY_POWER的实现则是直接调用PyNumber_Power()函数(不通过符号解析__builtin__模块里的"pow"的当前绑定)。cpython/ceval.c at 2.7 · python/cpython · GitHub

由于在Python代码里调用pow()实际上要先经过一次符号解析(LOAD_NAME)找到目标然后再调用过去,而模块的绑定又是可变的,所以可以做到下面这种事情:
<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>
很多人扯一大堆道理,然而对于本问题并没什么卵用。

真正原因在于,这是Python设计者的喜好罢了,因为设计者完全可以把floor也做成可以直接调用的嘛。 pow()是built-in function,所以不需要导入.
floor()是math module的function,需要import math(from math import floor)后才能用.
不过要注意的是math module中也有个pow(),和built-in的pow()有些不同.
可以看看官方文档:
2. Built-in Functions
以及
9.2. math — Mathematical functions -------------------------------Update------------------
可以看 @RednaxelaFX 的解释,我的理解有所偏差,还是放在前面吧,让更多人看到









上面说的很清楚,pow是Builtin函数,而Builtin函数是编译器直接支持的,可以参考这个链接了解Built in函数与普通函数的不同:Intrinsic function

下面我将顺便展开说说Python中是如何实现Builtin Pow的。首先在Python中,Built in函数定义在了Bltinmodules.c这个文件中,具体的代码在:

<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。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。