在python中进行编译和链接的过程是什么?
编译:python中的源代码保存为.py文件,然后将其编译为称为字节码的格式,然后将字节码转换为机器码。编译后,代码存储在.pyc文件中,并在更新源代码时重新生成。此过程称为编译。
链接:链接是最后一个阶段,其中所有功能都与它们的定义链接在一起,因为链接器知道在哪里实现所有这些功能。此过程称为链接。
示例
import dis def recursive_sum(n): """Function to return the sum of recursive numbers""" if n <= 1: return n else: return n + recursive_sum(n-1) # change this value for a different result number = 16 if number < 0: print("The sum is",recursive_sum(number)) # by using dis module ,the bytecode is loaded into machine code ,and a piece of code that reads each instruction in the bytecode and executes whatever operation is indicated. dis.dis(recursive_sum)
输出结果
The sum is 136 4 0 LOAD_FAST 0 (n) 2 LOAD_CONST 1 (1) 4 COMPARE_OP 1 (<=) 6 POP_JUMP_IF_FALSE 12 5 8 LOAD_FAST 0 (n) 10 RETURN_VALUE 7 >> 12 LOAD_FAST 0 (n) 14 LOAD_GLOBAL 0 (recursive_sum) 16 LOAD_FAST 0 (n) 18 LOAD_CONST 1 (1) 20 BINARY_SUBTRACT 22 CALL_FUNCTION 1 24 BINARY_ADD 26 RETURN_VALUE 28 LOAD_CONST 2 (None) 30 RETURN_VALUE