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开发编码规范
Sep 08 Python
python函数返回多个值的示例方法
Dec 04 Python
Python的Flask框架中Flask-Admin库的简单入门指引
Apr 07 Python
Python使用matplotlib简单绘图示例
Feb 01 Python
点球小游戏python脚本
May 22 Python
Python机器学习k-近邻算法(K Nearest Neighbor)实例详解
Jun 25 Python
Python常见数据结构之栈与队列用法示例
Jan 14 Python
解决windows下python3使用multiprocessing.Pool出现的问题
Apr 08 Python
Pytorch高阶OP操作where,gather原理
Apr 30 Python
Python执行时间的几种计算方法
Jul 31 Python
python list等分并从等分的子集中随机选取一个数
Nov 16 Python
详解python的异常捕获
Mar 03 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/12/06 PHP
JQuery模板插件 jquery.tmpl 动态ajax扩展
2011/11/10 Javascript
NodeJS的url截取模块url-extract的使用实例
2013/11/18 NodeJs
jquery根据属性和index来查找属性值并操作
2014/07/25 Javascript
JS实现太极旋转思路分析
2016/12/09 Javascript
详解package.json版本号规则
2019/08/01 Javascript
vscode 配置vue+vetur+eslint+prettier自动格式化功能
2020/03/23 Javascript
微信小程序点击生成朋友圈分享图(遇到的坑)
2020/06/17 Javascript
原生JS实现多条件筛选
2020/08/19 Javascript
通过滑动翻页效果实现和移动端click事件问题
2021/01/26 Javascript
[00:36]DOTA2勇士令状莱恩声望物品——冥晶之厄展示
2018/05/25 DOTA
使用python调用浏览器并打开一个网址的例子
2014/06/05 Python
python使用htmllib分析网页内容的方法
2015/05/08 Python
Python检测字符串中是否包含某字符集合中的字符
2015/05/21 Python
利用python获取某年中每个月的第一天和最后一天
2016/12/15 Python
python去除空格和换行符的实现方法(推荐)
2017/01/04 Python
Python模拟登陆实现代码
2017/06/14 Python
对python多线程中Lock()与RLock()锁详解
2019/01/11 Python
浅谈Pandas Series 和 Numpy array中的相同点
2019/06/28 Python
pytorch 实现删除tensor中的指定行列
2020/01/13 Python
利用Python裁切tiff图像且读取tiff,shp文件的实例
2020/03/10 Python
在Python中用GDAL实现矢量对栅格的切割实例
2020/03/11 Python
在python中实现求输出1-3+5-7+9-......101的和
2020/04/02 Python
Python 程序报错崩溃后如何倒回到崩溃的位置(推荐)
2020/06/23 Python
详解Python+Selenium+ChromeDriver的配置和问题解决
2021/01/19 Python
荷兰之家英文站:Holland at Home
2016/10/26 全球购物
匡威爱尔兰官网:Converse爱尔兰
2019/06/09 全球购物
音乐专业应届生教师求职信
2013/11/04 职场文书
开展读书活动总结
2014/06/30 职场文书
2014年团支部年度工作总结
2014/12/24 职场文书
对外汉语教师推荐信
2015/03/27 职场文书
2015年清剿火患专项行动工作总结
2015/07/27 职场文书
2016秋季幼儿园开学寄语
2015/12/03 职场文书
python读取pdf格式文档的实现代码
2021/04/01 Python
MySQL如何构建数据表索引
2021/05/13 MySQL
MongoDB日志切割的三种方式总结
2021/09/15 MongoDB