Python Basics 07-Module

Zero, article directory

Python Basics 07-Module

1. What is a module?

  • Python module (Module) is a Python file, ending with .py, containing Python object definitions and Python statements. Modules can define functions, classes and variables, and modules can also contain executable code.

2. Classification of modules

  • In Python, modules can usually be divided into two categories: built-in modules (currently used) and custom modules

3. How to import modules

(1) import module name

Basic syntax:

import 模块名称
import 模块名称1, 模块名称2, ...

Use the encapsulated methods in the module:

模块名称.方法()

Case: Use import to import the math module

import math

# 求数字9的平方根 = 3
print(math.sqrt(9))
(2) import module name as alias
  • In some cases, if the name of the imported module is too long, it is recommended to use the as keyword to rename it. We can use aliases when calling this module in the future.
import time as t

# 调用方式
t.sleep(10)
(3) from module name import *
  • Question: If you already have an import module, why do you still need to use the import method from module name to import function name?

  • Answer: Import means importing all functions in one or more modules, but in some cases, we only want to use certain methods under this module without importing them all. At this time, it is recommended to use from module name import function name

  • from module name import * import This import method means importing all functions of this module (equivalent to import module name)

(4) from module name import function name (recommended)
'''
导入方式:
from 模块名称 import *                      代表导入这个模块中所有函数
from 模块名称 import 函数1, 函数2, 函数3      代表仅导入函数123

调用方式:不需要模块名称,直接使用函数名称即可
'''
# from math import *
# 或
from math import sqrt, floor

# 调用方式
print(sqrt(9))
print(floor(10.88))
(5) time() method in time module
'''
time模块是一个与日期时间相关的模块,主要拥有这样两个方法
time.sleep(秒数) :休眠
time.time() :获取当前时间,返回的是一个数字,我们经常使用time.time()获取程序执行时间
'''
# 1、导入time模块
import time

# 2、程序开始前,获取一个时间点
start = time.time()
# 定义一个列表
list1 = []
for i in range(10000000):
    list1.append(i)
# 3、当程序执行结束,获取一个时间点
end = time.time()

print(f'以上程序执行一共消耗了{end - start}s时间!')

4. What is a custom module?

  • The essence of a module: In Python, theessence of a module is an independent Python file (suffix .py), which can contain Global variables, functions, and classes.
  • Note: In Python, each Python file can be used as a module, and the name of the module is the name of the file. In other words, the custom module name must comply with the identifier naming rules.
  • Special note: When we customize the module, the module name cannot be in Chinese or start with a number. In addition, our customized module name cannot conflict with the module name that comes with the system (such as os, random), otherwise the system module Function will not be available. For example, you cannot define a module called os.py

5. Custom module definition

  • Case: Create a custom file in a Python project, such as my_module1.py
# 封装一个函数sum_num(),专门对两个参数进行求和操作
def sum_num(num1, num2):
    print(num1 + num2)

6. Import custom modules

'''
在Python代码中,只要涉及到模块的导入你都有两种导入方式:① import ② from
'''
# import my_module1
# 导入模块中已经封装好的sum_num()方法
# my_module1.sum_num(10, 20)

from my_module1 import sum_num
# 调用方法
sum_num(10, 20)

8. Test module code

  • After we write the custom module, it is best to test the code in the module in advance to prevent any exceptions.

  • Introduces a magic variable:__name__, and its stored content is a string. With different running pages, the returned results are also different:

    • ① If__name__ is running on the current page, the return result is__main__
    • ② If__name__ is imported and run on a third-party page, the returned result is the module name (file name)
  • Based on the above characteristics, we can write__name__ in a custom module, with the following syntax:

    if __name__ == '__main__':
        # 执行测试代码
    
  • Define a custom module my_module2.py

'''
在Python中,以__name__这样命名的变量都属于魔术变量,拥有特殊的含义!
__name__ :是一个特殊的变量,随着运行环境的不同(随着所运行位置的不同,返回结果也是不同的)
__name__返回的结果是一个字符串
① 如果在本页面直接输出__name__,其结果为'__main__'
② 如果在本页面中,存在一个__name__,然后把这个页面导入到其他文件时,则__name__返回结果就是本页面的文件名称,不等于__main__
'''
print(__name__)
# print(type(__name__))
#
if __name__ == '__main__':
    print('如果以上条件成立,代表我们正在当前页面中执行此代码')
  • Run directly in the custom module and output
如果以上条件成立,代表我们正在当前页面中执行此代码
  • Introduce this module externally and run it, and the output
my_module2

Guess you like

Origin blog.csdn.net/liyou123456789/article/details/135039302