4

It appears that float.is_integer is the only "is" method with an underscore in its name among built-in types in Python. Examples that don't include an underscore: str.isalnum, str.isalpha, str.isdecimal, str.isdigit, str.isidentifier, str.islower, str.isnumeric, str.isprintable, str.isspace, str.istitle, str.isupper.

Any clues as to why?

By PEP 8, I would expect all these names to include an underscore. But practicality beats purity (PEP 20), so omitting the underscore in frequently used and short names makes sense. However, both naming conventions at once seems as a consequence of backward compatibility (with the logging module as the canonical example).

1
  • 2
    Maybe because float.is_integer is a more recent addition (as of 2.6), long after PEP8 has been established? The str apis were probably there since the beginning and were not changed due to compatibility reasons (though it probably could have in 3). float.fromhex could also potentially be renamed, I'm sure there's more cases to be found throughout the standard types. Commented Aug 27, 2017 at 9:17

1 Answer 1

2

A similar question has been asked on the Python bug tracker:

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.

And the answer (from R. David Murray, a Python core developer) there was:

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

Given the numbers in the question it seems like is_integer is not the only method with an underscore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.