module: as the program becomes more and more large, it needs to be divided into multiple files for ease of maintenance. This python allows the definition to be placed in a file and then imported 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.pydef 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);p rint (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);p rint (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);p rint (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. will get general information Instead, enter help (' ModuleName ') to obtain information on the specific module if provides the function name 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 ~
This article is from the "Hong Dachun Technical column" blog, please be sure to keep this source http://hongdachun.blog.51cto.com/9586598/1774160
Modules in Python