看起来 float.is_integer
是 Python 内置类型中唯一名称中带有下划线的“is”方法。 Examples that don't包括下划线:str.isalnum
、str.isalpha
、str.isdecimal
、str.isdigit
、str.isidentifier
、str.islower
、str.isnumeric
、str.isprintable
、str.isspace
code>, str.istitle
, str.isupper
.
关于原因的任何线索?
通过 PEP 8 , 我希望所有这些名称都包含下划线。但实用胜于纯粹 ( PEP 20 ),因此在常用名称和短名称中省略下划线是有道理的。然而,这两种命名约定似乎都是向后兼容的结果(以 logging
模块作为典型示例)。
最佳答案
A similar question已在 Python 错误跟踪器上被问到:
Compare
isinstance
,issubclass
, andislower
tois_integer
,is_fifo
, andis_enabled
. In Python 3.6, of all the names in the standard library starting withis
, 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/