首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

第10.4节 Python模块的弱封装机制

  • 23-09-22 18:30
  • 3832
  • 7221
blog.csdn.net

一、 引言
Python模块可以为调用者提供模块内成员的访问和调用,但某些情况下, 因为某些成员可能有特殊访问规则等原因,并不适合将模块内所有成员都提供给调用者访问,此时模块可以类似类的封装机制类似的模式提供一定的内部成员保护。
模块内的内部成员封装机制有两种,一种是定义类似类的私有成员,二是类似类的__slots__实例属性白名单机制,下面分别介绍这两种方法。

二、 定义模块的私有成员
在模块内,一个以下划线开头的成员属于模块的私有成员,在使用“from 模块名 import *”不会被导入,这和类的私有成员管理比较像,但还是有如下区别:
1、 下划线开头的成员包括了单下划线开头、双下划线开头、双下划线开头双下划线结尾的成员,它们的处理是一样的,而类中它们是不一样的,具体大家可参考《第7.8节 Python中隐秘的类封装方法》;
2、 模块的私有成员使用“from 模块名 import *”这样的语句来导入模块时,程序会跳过下划线开头的模块成员,即下划线开头的所有成员都不能“from 模块名 import *”导入,但可以通过“import 模块名”或“from 模块名 import 成员”方式导入。
Python通过这种简单的名字规则来隐藏成员的方法就是模块的封装机制,可以看到其实这种封装机制很脆弱。
3、 案例:
我们定义一个imptest.py,文件内容如下:

#imptest.py
def f():
    print("execute ftest function in imptest....")
def _f1():
    print("execute _f1(单下划线开头) function in imptest....")
def __f2():
    print("execute __f2(双下划线开头) function in imptest....")
def __f3__():
    print("execute __f3__(双下划线开头结尾) function in imptest....")
print("Now in imptest module!")    

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

1)使用“from 模块名 import *”导入并调用成员执行验证是否导入成功:

>>> from imptest import *
Now in imptest module!
>>> f()
execute ftest function in imptest....
>>> _f1()
Traceback (most recent call last):
  File "", line 1, in 
    _f1()
NameError: name '_f1' is not defined
>>> __f2()
Traceback (most recent call last):
  File "", line 1, in 
    __f2()
NameError: name '__f2' is not defined
>>> __f3__()
Traceback (most recent call last):
  File "", line 1, in 
    __f3__()
NameError: name '__f3__' is not defined
>>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

可以看到凡是带下划线的都没有导入。
2) 使用“import 模块名”导入并调用成员执行验证是否导入成功:

>>> import imptest
Now in imptest module!
>>> imptest.f()
execute ftest function in imptest....
>>> imptest._f1()
execute _f1(单下划线开头) function in imptest....
>>> imptest.__f2()
execute __f2(双下划线开头) function in imptest....
>>> imptest.__f3__()
execute __f3__(双下划线开头结尾) function in imptest....
>>>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

在这里插入图片描述

可以看到都能正常导入使用。
本节介绍了模块中使用下划线开头定义成员能防止“from 模块名 import *”导入这些成员,这是一种Python提供的弱封装机制,但建议大家在使用时还是遵循这种机制。

老猿Python,跟老猿学Python!
博客地址:http://iyenn.com/index/link?url=https://blog.csdn.net/LaoYuanPython

请大家多多支持,点赞、评论和加关注!谢谢!

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览333596 人正在系统学习中
老猿Python
微信公众号
专注Python相关语言、图像音视频处理、AI
注:本文转载自blog.csdn.net的LaoYuanPython的文章"https://blog.csdn.net/LaoYuanPython/article/details/98884058"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top