3 ms·
I'm surprised by your definition for a function named is_digit_char. I'd expect such a function to be something like: return len(s) == 1 and s[0] in string
by js2 13y ago
I'm surprised by your definition for a function named is_digit_char. I'd expect such a function to be something like:
return len(s) == 1 and s[0] in string.digits
Or
return re.search('^\d$', s)
Then it can be used idiomatically. The function you defined I'd call parse_int:
def parse_int(s)
"Return a parsed integer from 's' or None if it's not an int."""
...
Which would then be used as:
i = parse_int(s)
if i is None:
...your function definition...
$0.02.
- agentultra 13y agoI might try this... it wasn't actually named is_digit_char; I was actually writing a token parser for a "monadic" parser combinator library in Hy and my function was more idiomatically named there, integer-char? (after the CLHS function DIGIT-CHAR-P).