From 8ab78238179c8a24c695bb724c459aabddf13901 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 5 Mar 2026 18:50:40 -0500 Subject: [PATCH 1/2] shorten the __name__ __main__ embed --- bot/resources/tags/if-name-main.md | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/bot/resources/tags/if-name-main.md b/bot/resources/tags/if-name-main.md index 589c476189..b1479ff236 100644 --- a/bot/resources/tags/if-name-main.md +++ b/bot/resources/tags/if-name-main.md @@ -1,28 +1,20 @@ --- embed: - title: "`if __name__ == '__main__'`" + title: "`if __name__ == "__main__"`" --- -This is a statement that is only true if the module (your source code) it appears in is being run directly, as opposed to being imported into another module. When you run your module, the `__name__` special variable is automatically set to the string `'__main__'`. Conversely, when you import that same module into a different one, and run that, `__name__` is instead set to the filename of your module minus the `.py` extension. -**Example** -```py -# foo.py - -print('spam') +This is a convention for code that should run if the file is the main file of your program: -if __name__ == '__main__': - print('eggs') -``` -If you run the above module `foo.py` directly, both `'spam'`and `'eggs'` will be printed. Now consider this next example: ```py -# bar.py +def main(): + ... -import foo +if __name__ == "__main__": + main() ``` -If you run this module named `bar.py`, it will execute the code in `foo.py`. First it will print `'spam'`, and then the `if` statement will fail, because `__name__` will now be the string `'foo'`. -**Why would I do this?** +If the file is run directly, then the `main()` function will be run. +If the file is imported, it will not run. -- Your module is a library, but also has a special case where it can be run directly -- Your module is a library and you want to safeguard it against people running it directly (like what `pip` does) -- Your module is the main program, but has unit tests and the testing framework works by importing your module, and you want to avoid having your main code run during the test +For more about why you would do this and how it works, see +[`if __name__ == "__main__"`](https://pythondiscord.com/pages/guides/pydis-guides/if-name-main/). From 072181579b2797ffe14773bbd1656e629ad58c05 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 6 Mar 2026 06:24:30 -0500 Subject: [PATCH 2/2] fix quotes --- bot/resources/tags/if-name-main.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/resources/tags/if-name-main.md b/bot/resources/tags/if-name-main.md index b1479ff236..eb43c31f3b 100644 --- a/bot/resources/tags/if-name-main.md +++ b/bot/resources/tags/if-name-main.md @@ -1,6 +1,7 @@ --- +aliases: ["main"] embed: - title: "`if __name__ == "__main__"`" + title: '`if __name__ == "__main__"`' --- This is a convention for code that should run if the file is the main file of your program: