为什么在`float.is_integer`中使用下划线,而不是在'str.isnumeric`中?

问题描述 投票:4回答:1

似乎float.is_integer是唯一的“is”方法,其名称中的下划线在Python中的内置类型中。 Examples that don't包括下划线:str.isalnumstr.isalphastr.isdecimalstr.isdigitstr.isidentifierstr.islowerstr.isnumericstr.isprintablestr.isspacestr.istitlestr.isupper

任何线索为什么?

通过PEP 8,我希望所有这些名字都包含一个下划线。但实用性胜过纯度(PEP 20),因此省略常用和短名称中的下划线是有道理的。但是,这两种命名约定似乎都是向后兼容的结果(使用logging模块作为规范示例)。

python naming-conventions pep8
1个回答
2
投票

在Python bug跟踪器上询问了similar question

isinstanceissubclassisloweris_integeris_fifois_enabled进行比较。在Python 3.6中,在is开头的标准库中的所有名称中,我使用下划线计数69个名称,而不使用91个名称。选择一种方式或另一种方式似乎更好并坚持下去。我建议使用下划线,以便易读。

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

是的,那会很好。但随着时间的推移,Python已经发展,我们必须保持向后兼容性。这些名字就是它们的名字。

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

© www.soinside.com 2019 - 2024. All rights reserved.