Python's Path to growth "fifth article": Python-based module

Source: Internet
Author: User

Concepts of Modules & Package modules (module):

In the process of computer program development, as the program code more write more, in a file code will be more and more long, more and more difficult to maintain. In order to write maintainable code, we grouped many functions into separate files, so that each file contained relatively few code, and many programming languages used the existence of this kind of organization code. In Python, a . py file is called a module

What are the benefits of using modules?

The greatest benefit is that the maintainability of the code is greatly improved.

Second, writing code does not have to be zero-based, and when a module is written, it can be used elsewhere, and we often refer to other modules when writing programs, including Python built-in modules and modules from third parties.

There are three types of modules:
    • Python Standard library
    • Third-party modules
    • Application Custom Modules

In addition, the use of modules can also avoid the function name and variable name conflict, the same name of the functions and variables can be completely separate modules, so we do not have to consider the name when writing the module conflict with other modules, but also note that do not conflict with the built-in function name

Module Import Method 1, import statement
Import module name, module name

When we use the import statement, how does the Python interpreter find the corresponding file? The answer is that the interpreter has its own search path, which exists in Sys.path.

2. From...import statement
From ModName Import function name

This is equivalent to taking a function from the ModName module to the current one, and of course, the code in the ModName module is loaded again when the interpreter executes the code.

3. From...import * Statement
From ModName Import *

This provides an easy way to import all functions in a module, but this method is not recommended, and many Python programmers do not use this method, because the introduction of other sources of naming, it is likely to overwrite the existing definition

Python's Path to growth "fifth article": Python-based 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.