.py和.pyc文件有什么区别?
Python编译.py文件并将其另存为.pyc文件
.pyc包含Python源文件的编译字节码,不会为您执行的主程序文件创建.pyc(仅对于导入的模块)。
.pyc文件包含编码的python字节码。
如果要导入模块,则模块将计算两个数的加法
示例
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("Enter a positive number")
else:
print("The sum is",recursive_sum(number))输出结果
The sum is 136
如果您以“example”的名称存储该程序,那么当您运行example.py文件时,该程序将被存储为“example.py”,一旦代码首次执行,将需要一些时间来创建example.pyc文件。.py文件,其中代码首先由编译器以“example.pyc”文件的形式转换为字节码。