filmov
tv
Python Import Functionality

Показать описание
When the Python interpreter reads a source file, it executes all of the code found in it. This is called the direct execution as we are running codes directly from the source file. However, we could also run a python file from a different module or file by using the import functionality of python.
Let us assume we have two different modules: module1 & module2. In case we import module1 into module2, it just execute all the code present in the module1 and then move to the next line of code in module2, which does not have any code present in module1. Hence, no class or function, which we have defined in the module1, will be available for module2. If we wish to access them from module2, we need import them separately using below line of code.
from module import class_name, function_name
We could also assign a different name to all the classes and functions that we have imported from a different module, using 'as' keyword, as shown below.
from module import class_name as name1, function_name as name2
Every module in python has a special attribute called __name__. The value of __name__ attribute is set to '__main__' when module run as main program. Otherwise, the value of __name__ is set to contain the name of the module.
You could find this tutorial’s code in the following links:
Let us assume we have two different modules: module1 & module2. In case we import module1 into module2, it just execute all the code present in the module1 and then move to the next line of code in module2, which does not have any code present in module1. Hence, no class or function, which we have defined in the module1, will be available for module2. If we wish to access them from module2, we need import them separately using below line of code.
from module import class_name, function_name
We could also assign a different name to all the classes and functions that we have imported from a different module, using 'as' keyword, as shown below.
from module import class_name as name1, function_name as name2
Every module in python has a special attribute called __name__. The value of __name__ attribute is set to '__main__' when module run as main program. Otherwise, the value of __name__ is set to contain the name of the module.
You could find this tutorial’s code in the following links:
Комментарии