python 单下划线,双下划线使用

https://zhuanlan.zhihu.com/p/496153486

  1. 概述
    在Python经常能见到含下划线(underscore)修饰的的变量和方法(如__name__,_var等),这些下划线的作用称之为名字修饰(name decoration)。在Python中,名字修饰通常有以下几种情况:

单前缀下划线(single leading underscore):var
单后缀下划线(single trailingunderscore):var

双前缀下划线(double leading underscores):__var
双前缀+双后缀下划线(double leading & trailing underscores):var
除了名字修饰,在Python中下划线还有以下用法:

单独一个下划线
数字分隔符下划线
IPython中的特殊用途
我们对以上用法进行逐一详解。

  1. 名字修饰(name decoration)
    2.1 单前缀下划线
    方法和实例变量

Use one leading underscore only for non-public methods and instance variables.[1]
即,单前缀下划线用于私有的方法和实例变量。但Python和Java不同,并没有对公有和私有进行严格的区分。即便一个方法或者变量有单前缀下划线,也不影响被外界调用,它的作用仅限于一种“提示”(weak “internal use” indicator)。

class Test:

def __init__(self):
    self.a = "a"
    self._b = "b"

def _private_method(self):
    return ("This is a private method!")

单前缀下划线并不影响从外界调用

t = Test()
print(t.a)
print(t._b)
print(t._private_method)
导入

from M import *does not import objects whose names start with an underscore.[1]
即,当从另一个模块导入内容是,含前缀下划线的内容不会被导入。如

demo.py

a = "a"
_b = "b"

def _private_function():
return "This is a private function!"

from demo import *

print(a)
print(_b) # 会报错,私有变量无法导入
print(private_function) # 会报错,私有函数无法导入
2.2 单后缀下划线
single_trailing_underscore
: used by convention to avoid conflicts with Python keyword[1]
单后缀下划线主要是为了避免与一些Python关键字(如class,sum之类的)的冲突,如

tkinter.Toplevel(master, class_='ClassName')
2.3 双前缀下划线
To avoid name clashes with subclasses, use two leading underscores to invoke Python's name mangling rules.
Python mangles these names with the class name: if class Foo has an attribute named __a, it cannot be accessed by Foo.__a. (An insistent user could still gain access by calling Foo._Foo__a.) Generally, double leading underscores should be used only to avoid name conflicts with attributes in classes designed to be subclassed.[1]
双前缀下划线会触发Python中的名字改写规则(name mangling)什么意思呢?举个例子

class Test:
__a = "__a"

def __init__(self) -> None:
    self.__b = "__b"
    self._c = "_c"
    self.d = "d"

t = Test()
print(dir(t))
猜猜会打印什么呢?结果如下

name mangling
你会发现有_c和d,但是没有__a和__b。这是为什么呢?这是因为他们被改写成了_Test__a和_Test_b。因为名字被改写,所以不能通过t.__a访问,但是可以通过t._Test__a进行访问。

print(t.__a) # 会报错
print(t._Test__a) # 不会报错
为什么要这样设计呢?这样是为了避免在继承的时候,这些变量被重写,如

class Test:
__a = "__a"

class SubTest(Test):
__a = "change __a"

st = SubTest()
print(dir(st))
执行结果如下,如果在子类中重新定义__a时,会重新生成一个_SubTest__a,这样避免了父类中的_Test__a被改写。

除了_Test__a,还有_SubTest__a
2.4 双前缀+双后缀下划线
与双前缀下划线不同,双前缀+双后缀下划线并不会对名字进行改写。这些使用了双前缀+双后缀下划线的对象又被称为dunders,即Double UNDERScores的缩写。

魔法函数

double_leading_and_trailing_underscore: “magic” objects or attributes that live in user-controlled namespaces. E.g. init, import or file. Never invent such names; only use them as documented.
双前缀+双后缀下划线常用于“魔法函数”,表明这些函数被“官方占用”,不建议自行定义一个双前缀+双后缀下划线的对象,因为有可能与“官方用法”产生冲突。

模块

Module level “dunders” (i.e. names with two leading and two trailing underscores) such as__all__,author,version, etc. should be placed after the module docstring but before any import statements exceptfrom__future__imports. Python mandates that future-imports must appear in the module before any other code except docstrings:
除了魔法函数,一些模块也使用了双前缀+双后缀下划线,如__future__。Python要求的导入顺序是:__future__的import放在最前,然后是dunders的import,最后是普通的import。其中__future__的import强制放在最前,否则会报错。

"""
This is a demo! 这种docstrings放在最前
"""

from future import barry_as_FLUFL

all = ["test"]
version = '0.1'
author = 'Mr. Cheng'

import pandas as pd
3. 其他用法
3.1 单独一个下划线
当我们需要一个变量,但是又不需要在后面的程序中调用这个变量时,就可以用_,相当于告诉大家:这个变量无关紧要。最典型的例子就是用于for循环,如

for _ in range(10):
print("打印10次!")
也可以用作占位符对可迭代对象进行拆分,如

a, _, _, c = (1, 2, 3, 4)
a, _, _, c = [1, 2, 3, 4]
3.2 数字分隔符
在会计中,我们用逗号对较大的数字进行分隔,以方便识别,如93,123,110。但是在Python中,显然不能用逗号,但是可以用下划线,如:

a = 93_123_110
3.3 IPython中的特殊用途
在iPython中,下划线还有一个特殊用途:用以指代最近一个表达式的输出结果。如

posted @   LevisL  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示