4 ms·
>`str.is_digit()` doesn't 'check number', it checks if the string is entirely composed of unicode digit codepoints. What do you mean?
by noobie 11y ago
>`str.is_digit()` doesn't 'check number', it checks if the string is entirely composed of unicode digit codepoints.
What do you mean?
- epidemian 11y agoI'm not really sure. Maybe that: >>> u'0١߂३৪੫૬୭௮౯'.isdigit() True So it's not equivalent to matching [0-9]+, which some people might expect. It's quite nice i think, because apparently int() supports Unicode numeric characters too (i didn't know this): >>> int(u'0١߂३৪੫૬୭௮౯') 123456789
- sago 11y agoRight - `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