看來,float.is_integer
是唯一的「is」方法,它在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
。爲什麼`float.is_integer`中的下劃線,而不是`str.isnumeric`中的下劃線?
任何線索爲什麼?
通過PEP 8,我希望所有這些名稱都包含下劃線。但實用性勝過純度(PEP 20),所以在常用短名稱中省略下劃線是有道理的。然而,這兩種命名慣例似乎都是向後兼容的結果(以logging
模塊作爲典型例子)。
也許是因爲'float.is_integer'是PEP8建立後很久以前的一個新增加(2.6)? 'str' apis可能從一開始就存在,並且由於兼容性原因沒有改變(儘管它可能在3)。 'float.fromhex'也可能被重命名,我相信在整個標準類型中會有更多的案例可以找到。 –