python - 为什么 `float.is_integer` 中有下划线,但 `str.isnumeric` 中没有?

标签 python naming-conventions pep8

看起来 float.is_integer是 Python 内置类型中唯一名称中带有下划线的“is”方法。 Examples that don't包括下划线:str.isalnumstr.isalphastr.isdecimalstr.isdigitstr.isidentifierstr.islowerstr.isnumericstr.isprintablestr.isspace code>, str.istitle, str.isupper.

关于原因的任何线索?

通过 PEP 8 , 我希望所有这些名称都包含下划线。但实用胜于纯粹 ( PEP 20 ),因此在常用名称和短名称中省略下划线是有道理的。然而,这两种命名约定似乎都是向后兼容的结果(以 logging 模块作为典型示例)。

最佳答案

A similar question已在 Python 错误跟踪器上被问到:

Compare isinstance, issubclass, and islower to is_integer, is_fifo, and is_enabled. In Python 3.6, of all the names in the standard library starting with is, I count 69 names with the underscore and 91 without. It seems better to pick one way or the other and stick with it. I would recommend using the underscore, for legibility.

答案(来自 Python 核心开发人员 R. David Murray)是:

Yep, that would be nice. But Python has evolved over time, and we must maintain backward compatibility. The names are what they are.

鉴于问题中的数字,is_integer 似乎不是唯一带有下划线的方法。

关于python - 为什么 `float.is_integer` 中有下划线,但 `str.isnumeric` 中没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45903395/

相关文章:

python - 在 Python 中生成 3D 高斯分布

没有索引和项目的Python for循环

Python PEP8 打印没有缩进的包装字符串

python - 进行 train_step.run() 时出现 Tensorflow 错误

python - scikit 学习的特殊性

python - 使用外部 Python 程序在 OpenOffice 上加载文档

node.js - 哪些文件命名对于express.js 中的大型项目有好处?

naming-conventions - clojure 中如何使用 `*var-name*` 命名约定?

java - 单参数方法命名: repeat paramter in method name

python - PEP8 导入约定