How does Python read and interpret source files?

后端 未结 2 1837
后悔当初
后悔当初 2021-01-20 18:34

Say I run a Python (2.7, though I\'m not sure that makes a difference here) script. Instead of terminating the script, I tab out, or somehow switch back to my editing enviro

2条回答
  •  执笔经年
    2021-01-20 18:41

    First of all, you are indeed correct in your understanding that changes to a Python source file aren't seen by the interpreter until the next run. There are some debugging systems, usually built for proprietary purposes, that allow you to reload modules, but this bring attendant complexities such as existing objects retaining references to code from the old module, for example. It can get really ugly, though.

    The reason huge programs start up so quickly is the the interpreter tries to create a .pyc file for every .py file it imports if either no corresponding .pyc file exists or if the .py is newer. The .pyc is indeed the program compiled into byte code, so it's relatively quick to load.

    As far as JIT compilation goes you may be thinking of the PyPy implementation, which is written in Python and has backends in several different languages. It's increasingly being used in Python 2 shops where execution speed is important, but it's along way from the CPython that we all know and love.

提交回复
热议问题