Writing a string.ishex function

Vlastimil Brom vlastimil.brom at gmail.com
Fri Jan 15 12:53:47 EST 2010


2010/1/15 Duncan Booth <duncan.booth at invalid.invalid>:
> MRAB <python at mrabarnett.plus.com> wrote:
>
>> Duncan Booth wrote:
>>> MRAB <python at mrabarnett.plus.com> wrote:
>>>
>>>> I raise you one character:
>>>>
>>>> ishex2 = lambda s: not(set(s)-set(string.hexdigits))     # Yours
>>>> ishex3 = lambda s: not set(s)-set(string.hexdigits)      # Mine
>>>>
>>>> I could actually go three better:
>>>>
>>>> ishex3=lambda s:not set(s)-set(string.hexdigits)
>>>
>>> But none of those pass your own "ishex('') should return False" test.
>>
>> Neither do the others!
>
> That's true, but since you were the one that pointed out they were all
> broken I would have thought your solution should actually work.
>
> I'm sure you'll agree that a longer solution that works trumps any short
> but broken solution.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

ishex=lambda s:bool(re.match("[a-fA-F0-9]+$",s))
?
vbr



More information about the Python-list mailing list