Created
August 12, 2018 03:05
-
-
Save yuchen-xue/ff57bc2afe988d65af0330e3dc3b192e to your computer and use it in GitHub Desktop.
Example on understanding "__name__ == '__main__'" in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("This will always be shown first\n") | |
def main(): | |
print("Calling first_module's main method\n") | |
if __name__ == '__main__': | |
# the following will be shown only in `first_module.py` | |
main() | |
print("== Run directly ==\n") | |
else: | |
# the following will be shown only in `first_module.py` | |
print("== Run from import ==\n") | |
print("Name of the first_module: {}\n".format(__name__)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import first_module | |
# calling the `main()` function of `first_module` | |
first_module.main() | |
# this will only be shown in `second_module.py` | |
print("Name of the second_module: {}".format(__name__)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment