Python中的单下划线和双下划线使用场景详解


Posted in Python onSeptember 09, 2019

单下划线

单下划线用作变量

最常见的一种使用场景是作为变量占位符,使用场景明显可以减少代码中多余变量的使用。为了方便理解,_可以看作被丢弃的变量名称,这样做可以让阅读你代码的人知道,这是个不会被使用的变量,e.g.。

for _, _, filenames in os.walk(targetDir):
  print(filenames)
  
for _ in range(100):
  print('PythonPoint')

在交互解释器比如iPython中,_变量指向交互解释器中最后一次执行语句的返回结果。

单下划线前缀名称(例如_pythonPoint)

  • 这表示这是一个保护成员(属性或者方法),只有类对象和子类对象自己能访问到这些变量,是用来指定私有变量和方法的一种方式(约定而已)。如果使用from a_module import *导入时,这部分变量和函数不会被导入。不过值得注意的是,如果使用import a_module这样导入模块,仍然可以用a_module._pythonPoint这样的形式访问到这样的对象。
  • 另外单下划线开头还有一种一般不会用到的情况,例如使用一个C编写的扩展库有时会用下划线开头命名,然后使用一个去掉下划线的Python模块进行包装。如struct这个模块实际上是C模块_struct的一个Python包装。

单下划线后缀名称

通常用于和Python关键词区分开来,比如我们需要一个变量叫做class,但class是Python的关键词,就可以以单下划线结尾写作class_

双下划线

双下划线前缀名称

这表示这是一个私有成员(属性或者方法)。它无法直接像公有成员一样随便访问。双下划线开头的命名形式在Python的类成员中使用表示名字改编,即如果Test类里有一成员__x,那么dir(Test)时会看到_Test__x而非__x。这是为了避免该成员的名称与子类中的名称冲突,方便父类和子类中该成员的区分识别。但要注意这要求该名称末尾最多有一个下划线。e.g.

Python中的单下划线和双下划线使用场景详解

双下划线前缀及后缀名称

一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突。是一些Python的“魔术”对象,表示这是一个特殊成员。如类成员的__init____del____add__等,以及全局的__file____name__等。Python官方推荐永远不要将这样的命名方式应用于自己的变量或函数,而是按照文档说明来使用Python内置的这些特殊成员。

Python中关于私有属性、方法约定问题,官方文档如下

“Private” instance variables that cannot be accessed except from inside an object don't exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form__spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.

Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用htpasswd实现基本认证授权的例子
Jun 10 Python
pygame学习笔记(4):声音控制
Apr 15 Python
python使用正则表达式的search()函数实现指定位置搜索功能
Nov 10 Python
Python实现返回数组中第i小元素的方法示例
Dec 04 Python
Python3.6.2调用ffmpeg的方法
Jan 10 Python
详解pandas.DataFrame中删除包涵特定字符串所在的行
Apr 04 Python
使用python 将图片复制到系统剪贴中
Dec 13 Python
解决springboot yml配置 logging.level 报错问题
Feb 21 Python
Python带参数的装饰器运行原理解析
Jun 09 Python
python接口自动化之ConfigParser配置文件的使用详解
Aug 03 Python
利用Python pandas对Excel进行合并的方法示例
Nov 04 Python
新手必备Python开发环境搭建教程
May 28 Python
python 批量修改 labelImg 生成的xml文件的方法
Sep 09 #Python
Python定时发送天气预报邮件代码实例
Sep 09 #Python
python英语单词测试小程序代码实例
Sep 09 #Python
Python实现TCP通信的示例代码
Sep 09 #Python
Python3使用PySynth制作音乐的方法
Sep 09 #Python
python智联招聘爬虫并导入到excel代码实例
Sep 09 #Python
python 的 openpyxl模块 读取 Excel文件的方法
Sep 09 #Python
You might like
提升PHP执行速度全攻略(上)
2006/10/09 PHP
PHP从FLV文件获取视频预览图的方法
2015/03/12 PHP
PHP输入流php://input实例讲解
2015/12/22 PHP
Kindeditor编辑器添加图片上传水印功能(php代码)
2017/08/03 PHP
JS Replace 全部替换字符的用法小结
2013/12/24 Javascript
用JavaScript实现使用鼠标画线的示例代码
2014/08/19 Javascript
jQuery选择器querySelector的使用指南
2015/01/23 Javascript
同一个网页中实现多个JavaScript特效的方法
2015/02/02 Javascript
jQuery判断元素上是否绑定了指定事件的方法
2015/03/17 Javascript
JavaScript节点及列表操作实例小结
2015/08/05 Javascript
jquery实现左右无缝轮播图
2020/07/31 Javascript
jQuery模仿京东/天猫商品左侧分类导航菜单效果
2016/06/29 Javascript
总结JavaScript的正则与其他语言的不同之处
2016/08/25 Javascript
微信小程序 less文件编译成wxss文件实现办法
2016/12/05 Javascript
JS 实现banner图片轮播效果(鼠标事件)
2017/08/04 Javascript
vue addRoutes实现动态权限路由菜单的示例
2018/05/15 Javascript
浅谈JS对象添加getter与setter的5种方法
2018/06/09 Javascript
vue封装一个简单的div框选时间的组件的方法
2019/01/06 Javascript
Flutter 超实用简单菜单弹出框 PopupMenuButton功能
2019/08/06 Javascript
jquery validate 实现动态增加/删除验证规则操作示例
2019/10/28 jQuery
多个Vue项目部署到服务器的步骤记录
2020/10/22 Javascript
[01:14:31]Secret vs VG 2018国际邀请赛淘汰赛BO3 第一场 8.23
2018/08/24 DOTA
用Python代码来绘制彭罗斯点阵的教程
2015/04/03 Python
Python使用django搭建web开发环境
2017/06/09 Python
Django urls.py重构及参数传递详解
2019/07/23 Python
python爬虫 urllib模块反爬虫机制UA详解
2019/08/20 Python
如何给Python代码进行加密
2020/01/10 Python
基于python3生成标签云代码解析
2020/02/18 Python
Python基础教程(一)——Windows搭建开发Python开发环境
2020/07/20 Python
Python中读取文件名中的数字的实例详解
2020/12/25 Python
澳大利亚汽车零部件、音响及配件超市:Automotive Superstore
2018/06/19 全球购物
服装销售人员求职自我评价
2013/09/26 职场文书
幼儿师范毕业生自荐信
2013/11/09 职场文书
机械电子工程专业推荐信范文
2013/11/20 职场文书
开展党的群众路线教育实践活动方案
2014/02/05 职场文书
创业计划书之家政服务
2019/09/18 职场文书