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使用win32com实现的模拟浏览器功能示例
Jul 13 Python
python实现简单点对点(p2p)聊天
Sep 13 Python
Python算法输出1-9数组形成的结果为100的所有运算式
Nov 03 Python
python生成随机图形验证码详解
Nov 08 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
Feb 08 Python
详解python的argpare和click模块小结
Mar 31 Python
python opencv实现图像边缘检测
Apr 29 Python
python计算无向图节点度的实例代码
Nov 22 Python
Python绘制二维曲线的日常应用详解
Dec 04 Python
浅析Python 责任链设计模式
Sep 11 Python
Python提取视频中图片的示例(按帧、按秒)
Oct 22 Python
python中的sys模块和os模块
Mar 20 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
使用VisualStudio开发php的图文设置方法
2010/08/21 PHP
thinkphp的静态缓存用法分析
2014/11/29 PHP
Open and Print a Word Document
2007/06/15 Javascript
javascript 清除输入框中的数据
2009/04/13 Javascript
如何确保JavaScript的执行顺序 之实战篇
2011/03/03 Javascript
jquery异步请求实例代码
2011/06/21 Javascript
jquery入门—编写一个导航条(可伸缩)
2013/01/07 Javascript
使用EVAL处理jqchart jquery 折线图返回数据无效的解决办法
2015/11/26 Javascript
jquery实现网页定位导航
2016/08/23 Javascript
jQuery实现的手动拖动控制进度条效果示例【测试可用】
2018/04/18 jQuery
JavaScript常用数学函数用法示例
2018/05/14 Javascript
vuejs+element UI点击编辑表格某一行时获取内容填入表单的示例
2018/10/31 Javascript
微信小程序使用component自定义toast弹窗效果
2018/11/27 Javascript
详解JS取出两个数组中的不同或相同元素
2019/03/20 Javascript
Angular value与ngValue区别详解
2019/11/27 Javascript
JavaScript数组排序小程序实现解析
2020/01/13 Javascript
python将ansible配置转为json格式实例代码
2017/05/15 Python
利用TensorFlow训练简单的二分类神经网络模型的方法
2018/03/05 Python
PHP统计代码行数的小代码
2019/09/19 Python
python实现吃苹果小游戏
2020/03/21 Python
Python调用shell命令常用方法(4种)
2020/05/11 Python
用css3实现当鼠标移进去时当前亮其他变灰效果
2014/04/08 HTML / CSS
5分钟弄清楚html5的drag and drop(小结)
2019/04/10 HTML / CSS
英国优质鞋类专家:Robinson’s Shoes
2017/12/08 全球购物
介绍一下grep命令的使用
2015/06/12 面试题
银行办理业务介绍信
2014/01/18 职场文书
经典安踏广告词
2014/03/21 职场文书
工伤事故赔偿协议书
2014/04/15 职场文书
竞选班委演讲稿
2014/04/28 职场文书
给市场的环保建议书
2014/05/14 职场文书
活动总结结尾怎么写
2014/08/30 职场文书
党的群众路线教育实践活动总结
2014/10/30 职场文书
护士个人总结范文
2015/02/13 职场文书
超详细Python解释器新手安装教程
2021/05/10 Python
Redis基于Bitmap实现用户签到功能
2021/06/20 Redis
数据库之SQL技巧整理案例
2021/07/07 SQL Server