280
votes

Imagine a python script that will take a long time to run, what will happen if I modify it while it's running? Will the result be different?

1
The program is loaded into your main memory. If you change the source file, nothing happens. Imagine the CPU would read instructions from the hard drive... - Felix Kling
@Felix: That's called "Execute-in-Place" (XIP). - Ignacio Vazquez-Abrams
@Ignacio: Interesting, I didn't know that. Thanks :) - Felix Kling
You may dynamically reload the code of modules, see stackoverflow.com/questions/437589/… - Iliyan Bobev
Note that Windows batch files do execute in place, so this isn't a hypothetical question, there are languages out there that behave this way. - yoyo

1 Answers

323
votes

Nothing, because Python precompiles your script into a PYC file and launches that.

However, if some kind of exception occurs, you may get a slightly misleading explanation, because line X may have different code than before you started the script.