Modules in Python

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.