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中用PIL库批量给图片加上序号的教程
May 06 Python
python实现两个文件合并功能
Apr 01 Python
pandas数值计算与排序方法
Apr 12 Python
python3.x实现发送邮件功能
May 22 Python
Python/Django后端使用PIL Image生成头像缩略图
Apr 30 Python
python语言元素知识点详解
May 15 Python
jupyter notebook 实现matplotlib图动态刷新
Apr 22 Python
Python Scrapy图片爬取原理及代码实例
Jun 12 Python
python 使用递归的方式实现语义图片分割功能
Jul 16 Python
Python3.8安装Pygame教程步骤详解
Aug 14 Python
Python使用OpenCV和K-Means聚类对毕业照进行图像分割
Jun 11 Python
Python3使用Qt5来实现简易的五子棋小游戏
May 02 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的Yii 2框架的开发优势
2015/07/03 PHP
js下通过getList函数实现分页效果的代码
2010/09/17 Javascript
jQuery实现默认是闭合的FAQ展开效果菜单
2015/09/14 Javascript
详解JavaScript基于面向对象之创建对象(1)
2015/12/10 Javascript
第九章之路径分页标签与徽章组件
2016/04/25 Javascript
详解Angular Reactive Form 表单验证
2017/07/06 Javascript
利用Three.js如何实现阴影效果实例代码
2017/09/26 Javascript
vue2手机APP项目添加开屏广告或者闪屏广告
2017/11/28 Javascript
初试vue-cli使用HBuilderx打包app的坑
2019/07/17 Javascript
解决vue字符串换行问题(绝对管用)
2020/08/06 Javascript
[02:08]我的刀塔不可能这么可爱 胡晓桃_1
2014/06/20 DOTA
Python语言技巧之三元运算符使用介绍
2013/03/04 Python
Python中不同进制互相转换(二进制、八进制、十进制和十六进制)
2015/04/05 Python
python实现mysql的读写分离及负载均衡
2018/02/04 Python
解决Django中多条件查询的问题
2019/07/18 Python
python 字符串追加实例
2019/07/20 Python
解决Mac下使用python的坑
2019/08/13 Python
详解python中index()、find()方法
2019/08/29 Python
pycharm 代码自动补全的实现方法(图文)
2020/09/18 Python
python3中确保枚举值代码分析
2020/12/02 Python
css3实现冲击波效果的示例代码
2018/01/11 HTML / CSS
以实惠的价格提供高品质的时尚:Newchic
2018/01/18 全球购物
Michael Kors加拿大官网:购买设计师手袋、手表、鞋子、服装等
2019/03/16 全球购物
Pandora德国官网:购买潘多拉手链、戒指、项链和耳环
2020/02/20 全球购物
运动会解说词100字
2014/01/31 职场文书
自动化毕业生专业自荐书范文
2014/02/04 职场文书
运动会稿件100字
2014/02/21 职场文书
幼儿园爱国卫生月活动总结
2014/06/30 职场文书
汉语专业毕业生自荐信
2014/07/06 职场文书
毕业生实习期转正自我鉴定
2014/09/26 职场文书
师德师风个人自我剖析材料
2014/09/27 职场文书
国际贸易实务实训报告
2014/11/05 职场文书
创先争优个人总结
2015/03/04 职场文书
观看焦裕禄观后感
2015/06/09 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
python 镜像环境搭建总结
2022/09/23 Python