Python entry to proficient (9): module (Module) and package (Package)

1. Module (Module)

1.1. Introduction

In the process of computer program development, as more and more program codes are written, the amount of code in a file will be larger, and it will become more and more difficult to maintain in the future. We group many functions into separate files, so that each file contains relatively little code. Many programming languages ​​use this way of organizing code.

In Python, a .py file is a module.

Modules greatly improve code maintainability. Second, writing code doesn't have to start from scratch. When a module is written, it can be referenced elsewhere.

When we write programs, we often refer to other modules, including Python built-in modules and modules from third parties.

Using modules also avoids conflicting function names and variable names. Functions and variables with the same name can exist in different modules, so when we write our own modules, we don't have to consider that the names will conflict with other modules. But also pay attention to try not to conflict with built-in function names.

You might also think, what if different people write modules with the same name? In order to avoid module name conflicts, Python introduces a method of organizing modules by directory, called a package .

1.2, module import

Method 1. Format: import module name

import pygame

Method 2, format: from module name import method 1, method 2, ...

from plane_sprites import set name

Method 3. Format: from module name import *

from plane_sprites import *

1.3, the access entry of the program

python's built-in attribute: __name__

There are two types of return values ​​for this property

  • The first type: __main__, the program is actively executed, then the return result is this

  • The first type: file name, the program is executed after being called, then the return result is this

    def s1():
        print("hello")
    def s2():
        print("bye")
    name = "tom"
    age = 10
    
    if(__name__) == "__main__":     # 判断是否这个文件是主动执行
        s1()
        s2()
        print("------------------------------")

2. Package

2.1. Introduction

  • Put multiple python modules into a directory, this directory is a package

  • The package must contain a file __init__

2.2, the directory structure of the import package

# 目录结构
|---newpro.py           # 相当于主程序
|---package01           # 包
       |---__init__.py
   	   |---md1.py       # 包中的模块
       |---md2.py       # 包中的模块

2.3, package import

The essence of importing a package: importing a package is to execute the __init__.py file under the package

As long as there is an init.py file under a folder, then this folder can be regarded as a package.

The process of package import is basically the same as that of modules, except that when importing a package, the init.py in the package directory will be executed instead of the statements in the module. In addition, if the package is simply imported, and there are no other explicit initialization operations in the init.py of the package, the modules under this package will not be automatically imported.
Format: from package name import *

 from package01 import *

 

 

Guess you like

Origin blog.csdn.net/weixin_67281781/article/details/123734597