Module: As the program becomes more and more large for ease of maintenance you need to divide it into multiple files for this python allows you to put the definition into a file and import it as a module in another script
Create a module: Put 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 the 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 executes all statements related to the. py file in that namespace. To access the contents of a namespace after import, use the name of the module as a prefix: module.numvalue
If you want to import a module with a different name, 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 into 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 contents of a module into the current namespace, you can also use:
From module import *; A, B = NumValue (32,2);
As with objects, you can use the Dir () function to list the contents of a module
Import module; Print (dir (module));
Output Result:
[' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ', ' __loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' NumValue ']
How do I get help?
We've been in touch with __doc__. Get documentation in document Description Interactive mode when you run Python, you can use the Help () command to get information about the built-in modules and other aspects of Python, enter the general information separately and enter help (' ModuleName ') You can get information about a specific module if you provide a function name the Help () command can also return the details of the function
Summarize:
The concept of modules in 1,python: Saving the definition of a function and then importing it into other scripts;
2, defining the module: The file that holds the function definition should match the module name
3, importing modules: Importing using import statement if you want to import a module with a different name use the as qualifier
such as: Import module as fun;
To omit the prefix of a function in the calling module, you can import function-specific definitions using the From module import fun form
If you want to load all the definitions in the module, use the From module import *
4, List all the properties and methods in the module: Dir (module)
5. Use Help: How to use the helper () method ~
The above is the content of the module in Python, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!