3

I understand that Python interpreter will compiles source code during parsing just like what compiler for C does, which makes me curious why Python debugger can honor the editing during execution?

Say, using Pycharm I observed that, during a for loop the run-time behavior changes every time I edit the code, does it mean it re-compiles each time when a change is made? but I don't observe any delay/hang from it. Did I misunderstand concept?

7
  • The python interpreter does not compile just like a C compiler. It will execute line by line parsing as it goes. I would still expect it to make a copy of the source code before running to avoid the editing at runtime problem, but haven't tested. Commented Jun 5 at 10:47
  • @LukeSharkey, I got confused, if it is that case, why changing code during an non-debugger execution doesn't take effect? Commented Jun 5 at 10:51
  • Probably because it loads the whole file into memory and doesn't read the file line by line from the file system. The specific behavior is implementation defined. CPython has JIT compiler since 2024: peps.python.org/pep-0744 Commented Jun 5 at 10:52
  • @jabaa, that makes a lot sense, so internally, the interpreter parses and compiles line by line from RAM, it just the debugger does not give all files into RAM, but did it on-fly? Commented Jun 5 at 10:55
  • 1
    @LukeSharkey That's not how CPython works. It read the whole file and compiles it to byte-code, then interprets that. Commented Jun 5 at 14:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.