【Python基础】17.模块

模块

模块的概念

  • 每一个扩展名py结尾的Python源代码文件都是一个模块
  • 模块名同样也是一个标识符,需要符合标识符的命名规则
  • 在模块中定义的全局变量、函数、类都是可以提供给外界使用的工具
  • 模块就好比是工具包,想要使用这个工具包中的工具,就需要首先导入工具包

模块的导入方式

import导入

import 模块名1,模块名2

上述导入方式不是python PEP8推荐的导入方式

import 模块名1
import 模块名2

在导入模块时,每个导入应该独占一行
通过模块名.的方式使用模块

示例
新建module_test01.py文件
def sayhello():
    print("this is module-test 01")


class Module01:
    def __init__(self):
        print("this is an object from Module01")

新建module_test02.py文件

def sayhello():
    print("this is module-test 02")


class Module02:
    def __init__(self):
        print("this is an object from Module02")

新建module_test 文件
import module_test01
import module_test02

module_test01.sayhello()
module_test02.sayhello()

m1 = module_test01.Module01()
m2 = module_test02.Module02()

print(m1)
print(m2)
测试结果
this is module-test 01
this is module-test 02
this is an object from Module01
this is an object from Module02
<module_test01.Module01 object at 0x00000293A4501700>
<module_test02.Module02 object at 0x00000293A4501730>

使用别名导入

  • 如果模块名字太长或不符合使用习惯,可以使用as指定模块名
  • 模块别名符合大驼峰命名法
示例
修改module_test 文件
import module_test01 as module1
import module_test02 as module2

module1.sayhello()
module2.sayhello()

m1 = module1.Module01()
m2 = module2.Module02()
测试结果
this is module-test 01
this is module-test 02
this is an object from Module01
this is an object from Module02
<module_test01.Module01 object at 0x0000019EBD561850>
<module_test02.Module02 object at 0x0000019EBD561880>
  • 模块本质上还是原来的模块名,只是新增了个别名方便使用

from … import导入

  • 如果希望从某一个模块中,只导入需要的全局变量、方法、类,就可以使用from ... import导入方法
from 模块名 import 工具名
  • 导入之后
    1. 不需要通过模块名.的方式使用模块
    2. 可以直接使用模块中提供的全局变量、方法、类
示例
修改module_test 文件
from module_test01 import sayhello
from module_test02 import Module02

sayhello()
m2 = Module02()

print(m2)

测试结果
this is module-test 01
this is an object from Module02
<module_test02.Module02 object at 0x00000258371E0200>
对同名工具的处理
  • 如果导入的函数名称名称相同,后导入的函数,会覆盖之前导入的函数
修改module_test 文件
from module_test01 import sayhello
from module_test02 import sayhello
from module_test02 import Module02

sayhello()
m2 = Module02()

print(m2)


测试结果
this is module-test 02
this is an object from Module02
<module_test02.Module02 object at 0x000001AA0EFE16A0>
处理冲突
  • 发现冲突后,可以使用取别名的方式规避
修改module_test 文件
from module_test01 import sayhello
from module_test02 import sayhello as sayhello2

sayhello()
sayhello2()

运行结果
this is module-test 01
this is module-test 02
from … import *
from 模块名 import *

可以导入模块中的所有工具,并不用使用模块名.的方式使用工具,这种方法不推荐,因为函数重名没有提示,不好排查问题

修改module_test 文件
from module_test01 import *


sayhello()
m1 = Module01()

print(m1)


测试结果
this is module-test 01
this is an object from Module01
<module_test01.Module01 object at 0x000002E28704B710>

模块的搜索顺序

  1. 搜索当前工作目录下,指定名称的模块文件
  2. 如果没有,则会搜索系统目录

在开发时,给模块起名,需要避免与系统中模块名称重复

使用__file__属性查看当前模块的位置

import random
print(random.__file__)

输出结果

C:\Users\11\AppData\Local\Programs\Python\Python312\Lib\random.py

__name__属性

  • 一个独立的Python文件就是一个独立的模块
  • 在导入文件时,模块中所有没有缩进的代码都会被执行一遍

示例

修改module_test01.py文件
print("this is module_test01")


def sayhello():
    print("this is module-test 01")


class Module01:
    def __init__(self):
        print("this is an object from Module01")


修改module_test02.py文件
print("this is module_test02")


def sayhello():
    print("this is module-test 02")


class Module02:
    def __init__(self):
        print("this is an object from Module02")


修改module_test 文件
import module_test01
import module_test02
运行结果
this is module_test01
this is module_test02

判断__name__属性值,来确定是当前模块还是被导入的模块

  • __name__属性可以做到,测试代模块的代码只在测试情况下运行,而在被导入时不被执行
    1. __name__是python的内置属性,记录一个字符串
    2. 如果被其他文件导入时,__name__属性记录模块名
    3. 如果是当前文件运行,__name__ = '__main__'
修改module_test01.py文件
if __name__ == '__main__':
    print("this is module_test01")


def sayhello():
    print("this is module-test 01")


class Module01:
    def __init__(self):
        print("this is an object from Module01")


修改module_test02.py文件
if __name__ == '__main__':
    print("this is module_test02")


def sayhello():
    print("this is module-test 02")


class Module02:
    def __init__(self):
        print("this is an object from Module02")


修改module_test 文件
import module_test01
import module_test02
print("-"*10)
运行结果
----------

在很多python代码中可以看到以下格式代码

# 导入模块
# 定义全局变量
# 定义类
# 定义函数

# 定义main方法
def main():
    pass
# 判断```__name__```属性值,决定是否运行main方法
if __name__ == '__main__':
   main()
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值