Python中的下划线到底什么意思?

Python中的下划线到底什么意思?1 概述 在 Python 经常能见到含下划线 underscore 修饰的的变量和方法 如 name var 等 这些下划线的作用称之为名字修饰 name decoration 在 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 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  except from__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中,下划线还有一个特殊用途:用以指代最近一个表达式的输出结果。如

在学编程,学Python的小伙伴们,一个人摸黑学会很难,博主也是过来人, 这里新建了一个扣群:,给大家准备了学习资源、好玩的项目,欢迎大家加入一起交流。

小讯
上一篇 2025-04-04 11:58
下一篇 2025-03-21 13:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/59963.html