【Python】Python中的下划线到底什么意思?
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中的特殊用途
我们对以上用法进行逐一详解。
2. 名字修饰(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 byFoo.__a
. (An insistent user could still gain access by callingFoo._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))
猜猜会打印什么呢?结果如下
你会发现有_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被改写。
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"]