3 ms·
Right - `isdigit` checks for membership in unicode class Nd. So the other decimal digits you quote work. Similarly `int()` supports the same values. `isdigit` d
by sago 11y ago
Right - `isdigit` checks for membership in unicode class Nd. So the other decimal digits you quote work. Similarly `int()` supports the same values. `isdigit` doesn't support Nl or No class digits (not even ones corresponding to decimal digits, like Ⅳ).
I don't know if it is possible to construct an example where `isdigit` would return True but `int` would fail. It wouldn't surprise me, either way, but at the least the failure case isn't obvious.
The real problem is the converse `"1.23".isdigit()` returns False, where 1.23 is a number, so it doesn't 'check number'. And `int("-23")` works, but `"-23".isdigit()` is False. You could argue it 'checks non-negative integer'.
I happened upon a stack exchange answer a while back, accepted, that failed because of this bug.
In general the Python way to check if one thing can be converted into another is to try, and trap the error.
def isnumber(text):
try:
float(text)
except ValueError:
return False
else:
return True