源:
单下划线
单下划线用作变量
选择单元格区域,开始,字体,单击下划线图标右侧的向下的箭头,选择,双下划线。或 选择单元格区域,CTRL+1,字体,下划线,选择 双下划线,确定。
最常见的一种使用场景是作为变量占位符,使用场景明显可以减少代码中多余变量的使用。为了方便理解,_可以看作被丢弃的变量名称,这样做可以让阅读你代码的人知道,这是个不会被使用的变量,e.g.。
for _,_,filenames in os.walk(targetDir):
print(filenames)
for _ in range(100):
print('PythonPoint')
1、首先打开一个空白的Word文档。2、然后输入需要添加双下划线的文字。3、选中输入的文字后,点击鼠标右键,在下拉列表中选择“字体”。4、然后在弹出来的对话框中找到“所有文字”,再点击“下划线线型”下的复选框选择双线。
在交互解释器比如iPython中,_变量指向交互解释器中最后一次执行语句的返回结果。
单下划线前缀名称(例如_pythonPoint)
这表示这是一个保护成员(属性或者方法),只有类对象和子类对象自己能访问到这些变量,是用来指定私有变量和方法的一种方式(约定而已)。如果使用from a_module import *导入时,这部分变量和函数不会被导入。不过值得注意的是,如果使用import a_module这样导入模块,仍然可以用a_module._pythonPoint这样的形式访问到这样的对象。
单下划线后缀名称
双下划线
双下划线前缀名称
1、先用鼠标框选要添加双下划线的文字。2、点击word2016“开始”选项卡中下划线按钮右侧的小箭头。3、点击小箭头后会弹出下划线样式菜单,点击其中的双下划线。4、这样,之前选择的文字就添加双下划线了。5、另外,点击下划线。
这表示这是一个私有成员(属性或者方法)。它无法直接像公有成员一样随便访问。双下划线开头的命名形式在Python的类成员中使用表示名字改编,即如果Test类里有一成员__x,那么dir(Test)时会看到_Test__x而非__x。这是为了避免该成员的名称与子类中的名称冲突,方便父类和子类中该成员的区分识别。但要注意这要求该名称末尾最多有一个下划线。e.g.
双下划线前缀及后缀名称
一种约定,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.
一、打开Word,打开“开始菜单”,点击“下划线”箭头。二、展开下划线类型列表后,鼠标左键点击使用“双下划线”。三、这时输入一些空格符号,空格的下面就会出来一排连续的双横线。四、另一种方法,输入一些空格,选中这些空格。
Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.
以上~