Module: as the program becomes more and more convenient for maintenance, it needs to be divided into multiple files. Therefore, python allows you to put the definition into one file and then import it as a module in other scripts: as the program grows, it needs to be divided into multiple files for easy maintenance. For this reason, python allows the definition to be put into one file and then imported as a module in other scripts.
Create a module: place related statements and definitions in a file with the same name as the module
#file:module.py def numValue(a,b): q = a/b r = a-q*b return (q,r);
To use this module in other programs, you can use the import statement:
import module; a,b = module.numValue(64,8); print(a,b);
Output result:
8.0 0.0
The import statement creates a new namespace and runs. all the statements related to the py file need to access the namespace after the import, as long as the name of the module is used as the prefix, such as module. numValue
If you want to import the module with different names, you can add an optional as qualifier to the import statement.
import module as fun; a,b = fun.numValue(18,2); print(a,b);
Output result:
9.0 0.0
To import a specific definition to the current namespace, you can also use the following statement:
from module import numValue; a,b = numValue(32,2); print(a,b);
Output result:
16.0 0.0
To load all the content of the module to the current namespace, you can also use:
from module import *; a,b = numValue(32,2);
Like an object, you can use the dir () function to list the module content.
import module; print(dir(module));
Output result:
['_ Builtins _', '_ cached _', '_ doc _', '_ file __', '_ loader _', '_ name _', '_ package _', '_ spec _', 'numvalue']
How to get help?
We have already touched on the documents in the _ doc _ getting documentation. Instructions: when running python in interactive mode, you can use the help () command to obtain information about the built-in modules and other aspects of python. enter help () separately () general information will be obtained, while entering help ('modulename') will obtain information about the specific module. if The help () command of the function name is provided, details of the function can be returned.
Summary:
1. concept of a module in python: save the function definition and import and use it in other scripts;
2. define a module: the file for saving the function definition should be consistent with the module name.
3. import Module: Use the import statement to import the module with different names. you can use the as qualifier.
For example, import module as fun;
To omit the prefix when calling a function in a module, you can use the from module import fun format to import the specific definition of the function.
If you want to load all definitions in the module, use the from module import *
4. list all attributes and methods of the module: dir (module)
5. use the help () method ~
The above is the content of the python module. For more information, see PHP Chinese network (www.php1.cn )!