为什么在 `float.is_integer` 中有下划线,而在 `str.isnumeric` 中没有?
Why an underscore in `float.is_integer`, but not in `str.isnumeric`?
float.is_integer
is the only "is" method with an underscore in its name among built-in types in Python. Examples that don't 似乎包含下划线:str.isalnum
、str.isalpha
、str.isdecimal
、str.isdigit
、str.isidentifier
、str.islower
, str.isnumeric
, str.isprintable
, str.isspace
, str.istitle
, str.isupper
.
关于原因的任何线索?
By PEP 8, I would expect all these names to include an underscore. But practicality beats purity (PEP 20),因此省略常用名称和短名称中的下划线是有意义的。然而,这两种命名约定似乎都是向后兼容的结果(以 logging
模块作为典型示例)。
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
不是唯一带有下划线的方法。
float.is_integer
is the only "is" method with an underscore in its name among built-in types in Python. Examples that don't 似乎包含下划线:str.isalnum
、str.isalpha
、str.isdecimal
、str.isdigit
、str.isidentifier
、str.islower
, str.isnumeric
, str.isprintable
, str.isspace
, str.istitle
, str.isupper
.
关于原因的任何线索?
By PEP 8, I would expect all these names to include an underscore. But practicality beats purity (PEP 20),因此省略常用名称和短名称中的下划线是有意义的。然而,这两种命名约定似乎都是向后兼容的结果(以 logging
模块作为典型示例)。
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
不是唯一带有下划线的方法。