If Python is interpreted, what are .pyc files?

前端 未结 11 956
夕颜
夕颜 2020-11-22 08:39

I\'ve been given to understand that Python is an interpreted language...
However, when I look at my Python source code I see .pyc files, w

11条回答
  •  一整个雨季
    2020-11-22 09:21

    tldr; it's a converted code form the source code, which the python VM interprets for execution.

    Bottom-up understanding: the final stage of any program is to run/execute the program's instructions on the hardware/machine. So here are the stages preceding execution:

    1. Executing/running on CPU

    2. Converting bytcode to machine code.

      • Machine code is the final stage of conversion.

      • Instructions to be executed on CPU are given in machine code. Machine code can be executed directly by CPU.

    3. Converting Bytecode to machine code.

      • Bytecode is a medium stage. It could be skipped for efficiency, but sacrificing portability.
    4. Converting Source code to bytcode.

      • Source code is a human readable code. This is what is used when working on IDEs (code editors) such as Pycharm.

    Now the actual plot. There are two approaches when carrying any of these stages: convert [or execute] a code all at once (aka compile) and convert [or execute] the code line by line (aka interpret).

    • For example, we could compile a source code to bytcoe, compile bytecode to machine code, interpret machine code for execution.

    • Some implementations of languages skip stage 3 for efficiency, i.e. compile source code into machine code and then interpret machine code for execution.

    • Some implementations skip all middle steps and interpret the source code directly for execution.

    • Modern languages often involve both compiling an interpreting.

    • JAVA for example, compile source code to bytcode [that is how JAVA source is stored, as a bytcode], compile bytcode to machine code [using JVM], and interpret machine code for execution. [Thus JVM is implemented differently for different OSs, but the same JAVA source code could be executed on different OS that have JVM installed.]

    • Python for example, compile source code to bytcode [usually found as .pyc files accompanying the .py source codes], compile bytocde to machine code [done by a virtual machine such as PVM and the result is an executable file], interpret the machine code/executable for execution.

    • When we can say that a language is interpreted or compiled?

      • The answer is by looking into the approach used in execution. If it executes the machine code all at once (== compile), then it's a compiled language. On the other hand, if it executes the machine code line-by-line (==interpret) then it's an interpreted language.
    • Therefore, JAVA and Python are interpreted languages.

    • A confusion might occur because of the third stage, that's converting bytcode to machine code. Often this is done using a software called a virtual machine. The confusion occurs because a virtual machine acts like a machine, but it's actually not! Virtual machines are introduced for portability, having a VM on any REAL machine will allow us to execute the same source code. The approach used in most VMs [that's the third stage] is compiling, thus some people would say it's a compiled language. For the importance of VMs, we often say that such languages are both compiled and interpreted.

提交回复
热议问题