2

Let's say I input "A" to the Python program and run it. This would take some time. Rather than waiting and doing nothing, I change the input to "B" in the source code and run another instance of the program. The two instances will output some results when they're done. Would this work or would this mess up some stuff?

1
  • 1
    While this will work (as MattDMo explains) unless you've using some external resource (as Chris Cameron explains), why do it this way? Why not pass in that A or B through, e.g., a command-line argument, or a config file, so you don't have to edit the source? If you want to look at the contents of one directory, then look at the contents of a different directory, you don't edit the ls or dir command, do you? Commented Aug 12, 2014 at 3:48

2 Answers 2

3

This should work, assuming the permissions on your system allow you to edit a file that has already been started running - as abarnert indicates in his comment, on Windows your editor and the python.exe process may both try to lock the file while it's being used. When a Python script is started, the contents of the file are read into memory, so theoretically you should be able to then modify those contents and rerun the file again. However, these changes will not affect the initial run.

There are some cases where Python needs to read the source after execution has begun: a few instances are printing exceptions and various methods related to code introspection, for example. However, if you're just changing a small bit of information, such as a hard-coded source data file, or a beginning directory, there shouldn't be any issues. Ultimately, in the long run, if you find yourself doing this a lot, do as abarnert suggested up top and structure your program in such a way that you don't hard-code the information you need to change, and instead pass it as a command-line argument, config file, or dynamically-read parameter such as an input() (or raw_input(), in Python 2) statement.

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer, but a couple of minor quibbles. There are some circumstances where Python will try to read the original file on disk, most notably if it needs to print source code for, e.g., an exception traceback. (But in that case, the worst-case scenario is you get incorrect traceback printouts; you won't break any code unless it relies on introspecting source.) Also, it's not really permissions, but locking: on Windows, if your editor and/or Python want to open the file with exclusive locking, you won't be able to edit it while it's running; on any other platform, there's no problem.
@abarnert thanks for the clarifications. I was using the word "permissions" in the sense you were thinking (do you have "permission" to modify the file), but I see how UNIX permissions could apply as well (or not, as is actually the case).
1

This should work for trivial usage, but beware if both instances of your program are, for example, writing to the same file, or acting on a database of some kind. You may get unexpected results.

Comments

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.