单文件 python 脚本中函数名前的单下划线
single underscore before function name in single-file python script
我应该在单文件简单 python 脚本中在函数名前使用下划线吗?
例如,在这个脚本中,我应该在定义 f
和 g
之前添加下划线吗?此脚本不会从其他文件导入。
def f(x): # or _f(x)?
return x * 2
def g(x): # or _g(x)?
return x ** 2
def main():
x = f(100)
print(g(x))
if __name__ == "__main__":
main()
我在 python 中阅读了很多关于下划线用法的文档。他们中的许多人说下划线是关于 OOP 风格的编程以及 import
语句如何工作的。但是,在简单的单文件脚本中,我找不到好的答案。
什么是更好的模式?
_用于让开发者知道变量和方法是私有的,不应该被外部修改。因此,如果您想将功能保留在 class.
范围内,请使用 _
NOTE: using _ will not restrict the use of these methods or variables though, it's just a way of representation.
最流行的编程语言(例如Java
和C
/C++
)都有这种语法来声明private
attribute
和method
个 class 实例:
class MyClass {
private:
int _function() {
// Some code here
}
public:
bool _foo() {
// Some code here
}
}
Python
没有(而且可能永远不会有)这样的语法,所以他们只是创建了一个约定俗成的名称,让开发人员明白 method
是 private
并且永远不应从 class.
外部访问
根据PEP-8
:
We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work).
根据 Python 2
and Python 3
class 文档:
“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).
使用 _
可能对下面解释的技术有用。
根据Python 3
class documentation:
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.
注意:Python 文档主要谈论 “内部使用” 指的是 classes,但可以参考 module
s 也是,例如:
# main.py
import myModule
functionThatCanBeImported()
# myModule.py
def functionThatCanBeImported(): pass
def _functionThatShouldNeverBeImported(): pass
如果有人创建了 package
,他们不必在文档中放入 private
函数,因为它们用于 内部范围 和解释关于它们可能只对开发人员有用。
我应该在单文件简单 python 脚本中在函数名前使用下划线吗?
例如,在这个脚本中,我应该在定义 f
和 g
之前添加下划线吗?此脚本不会从其他文件导入。
def f(x): # or _f(x)?
return x * 2
def g(x): # or _g(x)?
return x ** 2
def main():
x = f(100)
print(g(x))
if __name__ == "__main__":
main()
我在 python 中阅读了很多关于下划线用法的文档。他们中的许多人说下划线是关于 OOP 风格的编程以及 import
语句如何工作的。但是,在简单的单文件脚本中,我找不到好的答案。
什么是更好的模式?
_用于让开发者知道变量和方法是私有的,不应该被外部修改。因此,如果您想将功能保留在 class.
范围内,请使用 _NOTE: using _ will not restrict the use of these methods or variables though, it's just a way of representation.
最流行的编程语言(例如Java
和C
/C++
)都有这种语法来声明private
attribute
和method
个 class 实例:
class MyClass {
private:
int _function() {
// Some code here
}
public:
bool _foo() {
// Some code here
}
}
Python
没有(而且可能永远不会有)这样的语法,所以他们只是创建了一个约定俗成的名称,让开发人员明白 method
是 private
并且永远不应从 class.
根据PEP-8
:
We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work).
根据 Python 2
and Python 3
class 文档:
“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).
使用 _
可能对下面解释的技术有用。
根据Python 3
class documentation:
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.
注意:Python 文档主要谈论 “内部使用” 指的是 classes,但可以参考 module
s 也是,例如:
# main.py
import myModule
functionThatCanBeImported()
# myModule.py
def functionThatCanBeImported(): pass
def _functionThatShouldNeverBeImported(): pass
如果有人创建了 package
,他们不必在文档中放入 private
函数,因为它们用于 内部范围 和解释关于它们可能只对开发人员有用。