Tag: python3.7

  • Python 底層運作 01 – 虛擬機器與 Byte Code

    Python 底層運作 01 – 虛擬機器與 Byte Code

    你可曾想過這段 Python 程式碼是如何運作的? >>> a = 5 >>> b = 10 >>> c = a * b >>> c 50 Python 作為 interpreted language,其運作可以分為兩個大項,Compiler 以及 Virtual Machine。Compiler 負責將輸入的語法做分析,轉換成 AST (Abstract Syntax Tree),再轉換成 CFG,最後依照 CFG 輸出 bytecode,Code Object 等必要的物件。Virtual Machine 則根據 bytecode 來運行,最後輸出程式運行的結果。 我們可以透過 Python 的 dis 模組,來得知程式碼的 Byte Code 是什麼: ➜ cpython git:(master) ✗ cat tests.py a…