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爬虫代理IP池实现方法
Jan 05 Python
python监控文件并且发送告警邮件
Jun 21 Python
Linux下python3.6.1环境配置教程
Sep 26 Python
PyTorch中常用的激活函数的方法示例
Aug 20 Python
Python使用matplotlib 模块scatter方法画散点图示例
Sep 27 Python
解决pycharm最左侧Tool Buttons显示不全的问题
Dec 17 Python
appium+python adb常用命令分享
Mar 06 Python
python实现交并比IOU教程
Apr 16 Python
python 通过文件夹导入包的操作
Jun 01 Python
Python实现弹球小游戏
Aug 01 Python
Python环境使用OpenCV检测人脸实现教程
Oct 19 Python
Python修改DBF文件指定列
Dec 19 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给图片加水印的实现代码
2020/04/18 PHP
PHP引用的调用方法分析
2016/04/25 PHP
详解yii2使用多个数据库的案例
2017/06/16 PHP
PHP ADODB实现事务处理功能示例
2018/05/25 PHP
准确获得页面、窗口高度及宽度的JS
2006/11/26 Javascript
js中escape对应的C#解码函数 UrlDecode
2012/12/16 Javascript
原生js实现给指定元素的后面追加内容
2013/04/10 Javascript
nodejs简单实现中英文翻译
2015/05/04 NodeJs
JavaScript实现数据类型的相互转换
2016/03/06 Javascript
使用jQuery中的wrap()函数操作HTML元素的教程
2016/05/24 Javascript
详解JavaScript节流函数中的Throttle
2016/07/16 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
关于layui flow loading占位图的实现方法
2019/09/21 Javascript
原生JS实现音乐播放器的示例代码
2021/02/25 Javascript
Python内置数据类型详解
2014/08/18 Python
Python多线程编程(三):threading.Thread类的重要函数和方法
2015/04/05 Python
python中快速进行多个字符替换的方法小结
2016/12/15 Python
Python读写/追加excel文件Demo分享
2018/05/03 Python
Python编程图形库之Pillow使用方法讲解
2018/12/28 Python
pymongo中group by的操作方法教程
2019/03/22 Python
python命令行参数用法实例分析
2019/06/25 Python
在linux下实现 python 监控usb设备信号
2019/07/03 Python
Python实现的微信红包提醒功能示例
2019/08/22 Python
Python基于百度AI实现OCR文字识别
2020/04/02 Python
CSS3感应鼠标的背景闪烁和图片缩放动画效果
2014/05/14 HTML / CSS
TripAdvisor土耳其网站:全球知名旅行社区,真实旅客评论
2017/04/17 全球购物
凯撒娱乐:Caesars Entertainment
2018/02/23 全球购物
英国一家集合了众多有才华设计师品牌的奢侈店:Wolf & Badger
2018/04/18 全球购物
世界上最大的隐形眼镜商店:1-800 Contacts
2018/11/03 全球购物
30年同学聚会感言
2014/01/30 职场文书
党的群众路线学习材料
2014/05/16 职场文书
大学生年度个人总结
2015/02/15 职场文书
刑事附带民事代理词
2015/05/25 职场文书
简短的人生哲理(38句)
2019/08/13 职场文书
创业计划书之零食店(进口)
2019/09/24 职场文书
MySQL主从复制断开的常用修复方法
2021/04/07 MySQL