Timeline for What does if __name__ == "__main__": do?
Current License: CC BY-SA 3.0
10 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 17, 2022 at 3:27 | comment | added | John | @Adam Rosenfield If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed.... What's the top-level code? The code with the indentation of zero? | |
| May 3, 2022 at 11:01 | review | Suggested edits | |||
| May 3, 2022 at 16:40 | |||||
| Feb 18, 2021 at 18:20 | comment | added | Merp |
So, if __name__ == "__main__": basically checks if you are running your python script itself, and not importing it or something?
|
|
| Jan 31, 2018 at 13:28 | history | edited | Tonechas | CC BY-SA 3.0 |
Changed __main__ to "__main__" in the last line
|
| S Jul 25, 2017 at 22:13 | history | suggested | Community Bot | CC BY-SA 3.0 |
Separate the two python files into separate blocks. Correct grammar mistake.
|
| Jul 25, 2017 at 18:22 | review | Suggested edits | |||
| S Jul 25, 2017 at 22:13 | |||||
| Jul 7, 2017 at 8:28 | review | Suggested edits | |||
| Jul 7, 2017 at 8:40 | |||||
| S Jul 5, 2017 at 19:19 | history | suggested | a learner has no name | CC BY-SA 3.0 |
I have tried to make it clearer why the code in the if-block is not being executed. It seems misleading to write the main() code would not be executed, as the main() code is being executed, but the if-condition (which is top-level code) is not met and thus the then-body is not being executed.
|
| Jul 5, 2017 at 13:56 | review | Suggested edits | |||
| S Jul 5, 2017 at 19:19 | |||||
| Jan 7, 2009 at 4:28 | history | answered | Adam Rosenfield | CC BY-SA 2.5 |