NEWBIE: ishexdigit revisited
Jeff Epler
jepler at unpythonic.net
Mon Dec 29 18:06:50 EST 2003
The only suggestion I would make is to skip calculating ix and instead
just use len(sx). The length of a string is already stored, so it
doesn't require Python to count the number of chars again (unlike C's
strlen()).
Well, actually, I'll make two suggestions: If it's an error in some
cases to have an extra nibble, I'd use a 'raise' statement instead of a
different return value, something like:
def ishexdigit(sx, silent=True):
for cx in sx:
if not cx in '0123456789abcdefABCDEF': return False
if silent or len(sx) % 2 == 0: return True
raise ValueError, "Extra nibble in '%s'" % sx
Now, ishexdigit('0') or ishexdigit('0', True) will return 1,
and ishexdigit('0', False) will cause an exception.
Jeff
More information about the Python-list
mailing list