User Tools

Site Tools


python:python_misc:python_modules_name_and_main

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
python:python_misc:python_modules_name_and_main [2022/10/22 01:49] – [Python modules, __name__ and "__main__"] mithatpython:python_misc:python_modules_name_and_main [2022/10/22 01:52] (current) – [What’s in a __name__?] mithat
Line 103: Line 103:
 ===== What’s in a __name__? ===== ===== What’s in a __name__? =====
  
-Every Python file that’s used in a program has a ''%%__name__%%'' variable associated with it. When a file is imported as a module, the file’s ''%%__name__%%'' variable is set to a string containing the name of the module. So, in the example above, the ''%%__name__%%'' variable of the ''%%my_functions%%'' module while when it's imported into our program is ''%%'my_functions'%%''.+Every Python file that’s used in a program has a ''%%__name__%%'' variable associated with it. When a file is imported as a module, the file’s ''%%__name__%%'' variable is automatically set to a string containing the name of the module. So, in the example above, the ''%%__name__%%'' variable of the ''%%my_functions%%'' module while when it's imported into our program is ''%%'my_functions'%%''.
  
-However, when the file is //not// imported as a module, which is to say when a file is run as a program, its ''%%__name__%%'' variable is set to the somewhat cryptic string ''%%'__main__'%%''. This is done so that at runtime, the we can determine whether a file is being imported as a module or is being run as a program. If a file is being run as a program, the condition:+However, when the file is //not// imported as a module, which is to say when a file is run as a program, its ''%%__name__%%'' variable is instead set to the somewhat cryptic string ''%%'__main__'%%''. This is done so that at runtime we can determine whether a file is being imported as a module or is being run as a program. If a file is being run as a program, the condition:
  
 <code python> <code python>
python/python_misc/python_modules_name_and_main.1666403354.txt.gz · Last modified: 2022/10/22 01:49 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki