Whenever the Python interpreter interacts with an import or equivalent statement, it does three operations, which are described in the next sections.
Loading a module
The Python interpreter searches for the specified module on a sys.path (to be discussed in the Accessing packages from any location section) and loads the source code. This has been explained in the Learning how import works section.
Setting special variables
In this step, the Python interpreter defines a few special variables, such as __name__, which basically defines the namespace that a Python module is running in. The __name__ variable is one of the most important variables.
In the case of our example of the calcmain1.py, mycalculator.py, and myrandom.py modules, the __name__ variable will be set for each module as follows:
There are two cases of setting the __name__ variable, which are described next.
Case A – module as the main program
If you are running your module as the main program, the __name__ variable will be set to the __main__ value regardless of whatever the name of the Python file or module is. For example, when calcmain1.py is executed, the interpreter will assign the hardcoded __main__ string to the __name__ variable. If we run myrandom.py or mycalculator.py as the main program, the __name__ variable will automatically
get the value of __main__.
Therefore, we added an if __name__ == '__main__' line to all main scripts to check whether this is the main execution program.
Case B – module is imported by another module
In this case, your module is not the main program, but it is imported by another module. In our example, myrandom and mycalculator are imported in calcmain1.py. As soon as the Python interpreter finds the myrandom.py and mycalculator.py files, it will assign the myrandom and mycalculator names from the import statement to the __name__ variable for each module. This assignment is done prior to executing the code inside these modules. This is reflected in Table shown above.
Executing the code
After the special variables are set, the Python interpreter executes the code in the file line by line. It is important to know that functions (and the code under the classes) are not executed unless they are not called by other lines of code. Here is a quick analysis of the three modules from the execution point of view when calcmain1.py is run:
• mycalculator.py: After setting the special variables, there is no code to be executed in this module at the initialization time.
• myrandom.py: After setting the special variables and the import statement, there is no further code to be executed in this module at initialization time.
• calcmain1.py: After setting the special variables and executing the import statements, it executes the following if statement: if __name__ == "__main__":. This will return true because we launched the calcmain1.py file.
Inside the if statement, the my_main () function will be called, which in fact then calls methods from the myrandom.py and mycalculator.py modules.
We can add an if __name__ == "__main__" statement to any module regardless of whether it is the main program or not. The advantage of using this approach is that the module can be used both as a module or as a main program. There is also another application of using this approach, which is to add unit tests within the module. Some of the other noticeable special variables are as follows:
• __file__: This variable contains the path to the module that is currently being imported.
• __doc__: This variable will output the docstring that is added in a class or a method. A docstring is a comment line added right after the class or method definition.
• __package__: This is used to indicate whether the module is a package or not. Its value can be a package name, an empty string, or none.
• __dict__: This will return all attributes of a class instance as a dictionary.
• dir: This is actually a method that returns every associated method or attribute as a list.
• Locals and globals: These are also used as methods that display the local and global variables as dictionary entries.
0 comments:
Post a Comment