Bifalgorithm post
What is double underscore name equals double underscore main in Python? First of all, it is just a variable called name, and every file in Python has it set by default. If I write print double underscore name and run the file, you can see it prints double underscore main. This happens because we are running this file directly, so Python knows this is the main file being executed. But if we import this same file into another Python program and run it again, you will see a different result. This time, it prints the name of the file instead of double underscore. This happens because Python now understands that this file is not the main program anymore, it is just being imported and used inside another file. That is why name equals main is used, to control which code should run only when the file is executed directly, and not when it is imported. You can also use the dir function to understand this better. If you print dir inside a Python file, it shows all the default names Python creates for that file. In that list, you will see double underscore name, which shows that name is already provided by Python and not something we define ourselves.
What is double underscore name equals double underscore main in Python? First of all, it is just a variable called name, and every file in Python has it set by…
Ideas in this flowchart
- Double Underscore __name__ == '__main__' in Python
- __name__ Variable
- Default Setting
- Direct Execution
- Prints '__main__'
- File is Main
- Imported Execution
- Prints File Name
- File is Not Main
- Control Code Execution
- dir() Function
- List Default Names
- __name__ in List