


在 Python 中,为什么 pow 这样的函数可以直接调用,而 floor 这样的函数得先导入模块?
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)找到目标然后再调用过去,而模块的绑定又是可变的,所以可以做到下面这种事情:
<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><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><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><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>
>>> __builtins__ <module '__builtin__' (built-in)> >>> 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 >>>
真正原因在于,这是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这个文件中,具体的代码在:
<span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">builtin_methods</span><span class="p">[]</span>

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Tiada aplikasi yang boleh menukar semua fail XML ke dalam PDF kerana struktur XML adalah fleksibel dan pelbagai. Inti XML ke PDF adalah untuk menukar struktur data ke dalam susun atur halaman, yang memerlukan parsing XML dan menjana PDF. Kaedah umum termasuk parsing XML menggunakan perpustakaan python seperti ElementTree dan menjana PDF menggunakan perpustakaan ReportLab. Untuk XML yang kompleks, mungkin perlu menggunakan struktur transformasi XSLT. Apabila mengoptimumkan prestasi, pertimbangkan untuk menggunakan multithreaded atau multiprocesses dan pilih perpustakaan yang sesuai.

Permohonan yang menukarkan XML terus ke PDF tidak dapat dijumpai kerana mereka adalah dua format yang berbeza. XML digunakan untuk menyimpan data, manakala PDF digunakan untuk memaparkan dokumen. Untuk melengkapkan transformasi, anda boleh menggunakan bahasa pengaturcaraan dan perpustakaan seperti Python dan ReportLab untuk menghuraikan data XML dan menghasilkan dokumen PDF.

Gunakan kebanyakan editor teks untuk membuka fail XML; Jika anda memerlukan paparan pokok yang lebih intuitif, anda boleh menggunakan editor XML, seperti editor XML oksigen atau XMLSPY; Jika anda memproses data XML dalam program, anda perlu menggunakan bahasa pengaturcaraan (seperti Python) dan perpustakaan XML (seperti XML.Etree.ElementTree) untuk menghuraikan.

Pengindahan XML pada dasarnya meningkatkan kebolehbacaannya, termasuk lekukan yang munasabah, rehat garis dan organisasi tag. Prinsipnya adalah untuk melintasi pokok XML, tambah lekukan mengikut tahap, dan mengendalikan tag dan tag kosong yang mengandungi teks. Perpustakaan XML.Etree.ElementTree Python menyediakan fungsi Pretty_XML yang mudah yang dapat melaksanakan proses pengindahan di atas.

Kelajuan XML mudah alih ke PDF bergantung kepada faktor -faktor berikut: kerumitan struktur XML. Kaedah Penukaran Konfigurasi Perkakasan Mudah Alih (Perpustakaan, Algoritma) Kaedah Pengoptimuman Kualiti Kod (Pilih perpustakaan yang cekap, mengoptimumkan algoritma, data cache, dan menggunakan pelbagai threading). Secara keseluruhannya, tidak ada jawapan mutlak dan ia perlu dioptimumkan mengikut keadaan tertentu.

Tidak mustahil untuk menyelesaikan penukaran XML ke PDF secara langsung di telefon anda dengan satu aplikasi. Ia perlu menggunakan perkhidmatan awan, yang boleh dicapai melalui dua langkah: 1. Tukar XML ke PDF di awan, 2. Akses atau muat turun fail PDF yang ditukar pada telefon bimbit.

XML boleh ditukar kepada imej dengan menggunakan perpustakaan penukar XSLT atau imej. XSLT Converter: Gunakan pemproses XSLT dan stylesheet untuk menukar XML ke imej. Perpustakaan Imej: Gunakan perpustakaan seperti PIL atau ImageMagick untuk membuat imej dari data XML, seperti bentuk lukisan dan teks.

Untuk menjana imej melalui XML, anda perlu menggunakan perpustakaan graf (seperti bantal dan JFreechart) sebagai jambatan untuk menjana imej berdasarkan metadata (saiz, warna) dalam XML. Kunci untuk mengawal saiz imej adalah untuk menyesuaikan nilai & lt; lebar & gt; dan & lt; ketinggian & gt; Tag dalam XML. Walau bagaimanapun, dalam aplikasi praktikal, kerumitan struktur XML, kehalusan lukisan graf, kelajuan penjanaan imej dan penggunaan memori, dan pemilihan format imej semuanya mempunyai kesan ke atas saiz imej yang dihasilkan. Oleh itu, perlu mempunyai pemahaman yang mendalam tentang struktur XML, mahir dalam perpustakaan grafik, dan mempertimbangkan faktor -faktor seperti algoritma pengoptimuman dan pemilihan format imej.
