Search code examples
pythonvm-implementation

Does the python vm compiles method every time?


If I have a function that is called in few places in my module, does the virtual machine compiles it to native code only the first time the function is executed and than use the cashed code on the other calls? (like .NET jit compiler)


Solution

  • In CPython (the standard Python implementation) the first time a Python module is imported, it's compiled to bytecode and stored in a .pyc file. From then on, the .pyc file is read and interpreted by the VM when needed. Once the .pyc is read into memory, the bytecode is in memory, and interpreted by the VM when the function is called.

    CPython never compiles Python code to native executable code.