The Python language module

Source: Internet
Author: User

A module is basically a file that contains all of the functions and variables you define. It is useful to enable you to reuse the features and services it provides in other programs.

1. How to use the module

The file name of the module must be a. py extension.

When we need to use a module, we need to import it in the current module. Take system standard library SYS as an example:

1 Import SYS 2 3  for inch SYS.ARGV: 4     Print (i) 5 6 Print '  ', Sys.path)

You can also use the From...import statement, but this method is not recommended.

The meaning of the specific variables in the SYS module is explained in the Python Library series.

2.__name__ Property

When a module is entered for the first time, the main block of the module will be run. When we want to run the main block only when the program itself is being used, and not run the main block when it is called, it can be done through the __name__ property of the module.

1 #Filename: myself.py 2 if ' __main__ ' : 3     Print ('run by myself')4Else:5      Print ('run by others')

Results:

$python Myself.pyrun by Myself$python>>>Import Myselfrun by others

The Python language module

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.