你能解释一下 Python 基础库中函数的奇怪和不一致的命名吗?

a_r*_*rts 6 python standards naming-conventions

当我开始学习 Python 时,我开始喜欢它,因为它比 PHP 结构化了很多,PHP 有很多功能不是很流畅,但我一直注意到 Python 中没有明显推理的奇怪不一致。

例如,在 PHP 中有一些数组函数的名称以“array”开头,一些没有,一些使用下划线,一些将其名称的一部分缩短为单个字符,等等。它们通常需要将数组作为参数而不是数组对象的方法。

在 Python 中,有很多单字函数名,但是当涉及到多个字时,我看到了不一致。
例如,带有驼峰式样的日志模块方法,类似logging.StreamHandler()sys 中的下划线sys.base_prefix()和没有分隔符的小写,如os.expandvars().
好像这还不够,有这样的函数名称os.path.splitext()最终导致我发布了这个。

为什么它们不都是一种约定?

logging.StreamHandler() # capitalize in case classes
sys.basePrefix()
os.expandVars()
os.path.splitText()
Run Code Online (Sandbox Code Playgroud)

甚至像这样:

logging.stream_handler()
sys.base_prefix()
os.expand_vars()
os.path.split_text() # the original is actually "spli + text" in one word!
Run Code Online (Sandbox Code Playgroud)

是否有任何流行的编程语言严格遵守下面的示例中的约定?

some_value        # variable lower case separated by underscores (which allows them to appear descriptive)
someFunction()    # functions and methods camel case, first letter lower case (differentiates from variables while still readable and allows simple names like get() and send())
SomeObject()      # classes always start with capital letters and are camelcase (makes them stand out and above but appear closer to functions)
IMPORTANT_VALUE   # constants always upper case, separated by underscores (easily tell apart from anything else, while being the reverse case from normal variables)
Run Code Online (Sandbox Code Playgroud)

那么为什么会发生这种情况,并且像我的例子这样的事情在现实中是否会发生重大变化?

小智 -4

这是个好问题。我相信这是因为人们可以给它们命名,所以对于某些效率胜过可读性,而另一些则反之亦然。