From ab33f74bcbfe3c6d8a1c55e8514419b9b50f709d Mon Sep 17 00:00:00 2001 From: PinkR1ver <3180102330@zju.edu.cn> Date: Wed, 6 Mar 2024 10:19:31 +0800 Subject: [PATCH] Add note --- .../python/underline_in_python.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 content/computer_sci/coding_knowledge/python/underline_in_python.md diff --git a/content/computer_sci/coding_knowledge/python/underline_in_python.md b/content/computer_sci/coding_knowledge/python/underline_in_python.md new file mode 100644 index 000000000..ca7dd90ab --- /dev/null +++ b/content/computer_sci/coding_knowledge/python/underline_in_python.md @@ -0,0 +1,20 @@ +--- +title: underline in python +tags: + - coding-language + - python +date: 2024-03-05 +--- +在 Python 中,你可以在变量名中使用下划线,但下划线的位置和数量会影响变量的含义。 + +1. **单下划线 `_`**: + - 通常用作临时或无关紧要的变量名。例如,`_` 可以用于临时存储某个函数的返回值,但不打算在后续代码中使用它。 + - 这是一种约定,告诉其他开发人员这个变量不是主要关注点。 +2. **双下划线 `__`**: + - 在类定义中,双下划线用于名称修饰(name mangling)。它将变量名变成私有的,以避免与子类中的同名变量冲突。 + - 例如,如果你在类中定义了一个变量 `__my_var`,Python 会将其重命名为 `_ClassName__my_var`,以确保它不会被子类意外覆盖。 +3. **单下划线后跟单个字符(例如 `_x`)**: + - 这通常表示一个临时变量,或者在某些上下文中表示“不关心”的变量。 +4. **在导入语句中的下划线**: + - 在示例代码片段中,`from .explainers._additive import AdditiveExplainer` 中的下划线表示模块或包的内部结构。 + - 通常,以单下划线开头的模块或变量是内部使用的,不应该直接从外部导入或访问。 \ No newline at end of file