例如,在PHP中,有一些数组函数的名称以"array"开头,有些则没有,有些使用下划线,有些将名称的某些部分缩短为单个字符等等,它们通常需要将一个数组作为参数,而不是数组对象的方法。
在Python中,有很多单词函数名,但涉及到多个单词时,我看到了不一致性。例如,logging模块的方法使用驼峰命名法,如logging.StreamHandler(),sys中使用下划线,如sys.base_prefix(),而os中则直接使用小写字母而没有分隔符,如os.expandvars()。如果这还不够,就会出现像os.path.splitext()这样的函数名,最终导致我发布了这篇文章。
为什么它们不都遵循同一种约定呢?
logging.StreamHandler() # capitalize in case classes
sys.basePrefix()
os.expandVars()
os.path.splitText()
甚至可以像这样:
logging.stream_handler()
sys.base_prefix()
os.expand_vars()
os.path.split_text() # the original is actually "spli + text" in one word!
是否存在任何流行的编程语言严格遵守类似于以下示例中的约定?
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)
那么为什么会发生这种情况?类似于我的例子,在现实中是否经常发生这种情况?
tolist()
真的让我很烦恼,因为其他所有东西都是to_csv()
等(尽管不是基本库)。现在我已经抱怨过了,因为人类是不一致的,这也许就是“为什么会发生这种情况”的答案。 - roganjoshsplitext
不是split text
的奇怪缩写,而是split ext
(分割扩展名)的缩写,所以这是有意义的。否则,我完全同意Python中存在命名不一致的问题。 - domsson