Writing a string.ishex function

S

Steven D'Aprano

Don't use 'is', use '=='.

I beg to differ. In this case, since ishex is intended to return True or
False and not 1.0 or Decimal(0), the correct test for the purposes of
unit-testing is the identity test, not equality.

However, for the purposes of *displaying* the result, rather than saying:

print ishex('123') is True

one should obviously say:

print ishex('123') is True is True

No, wait, that should be:

print ishex('123') is True is True is True

No wait, this is better:

print ishex('123') is True is True is True is True

*wink*

Seriously, just say print ishex('123'). It already returns True or False,
you don't need to compare it to True or False to get a True or False
answer.

BTW, ishex('') should return False.


Only if you want to be inconsistent with other isFoo string functions:
False


Also, if ishex(s) returns True, then a reasonable person will expect that
calling int(s, 16) should succeed. But if s is the empty string, it will
fail.
 
W

Wolfram Hinderer

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)

:)

ishex4=lambda s:not s.strip(string.hexdigits)
 
V

Vlastimil Brom

2010/1/15 Duncan Booth said:
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.

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

MRAB

Duncan said:
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.

I believe that the rules of the character-counting game don't require
correctness, only that it's no worse.

A real solution would use 'def', have a docstring, etc, of course.
 
S

Steven D'Aprano

Only if you want to be inconsistent with other isFoo string functions:

False

I said what???

Sorry MRAB, what I wrote there was obvious nonsense. I should be agreeing
with you, not disagreeing!
 
A

Albert van der Horst

Don't use 'is', use '=='.

BTW, ishex('') should return False.

You are very wrong. Not with the above statement, but
the very act of issuing a statement like that is wrong.

The OP didn't specify ishex().
In absence of a specification, border cases are not defined.

If the specification was:
any character of string s must be a hex character

then ishex('') should return True.

If the specification was:
the string must represent a hex number

then ishex('') should probably return False.

I can imagine a specification where it is appropriate to
throw an exception for an empty string.
This is also the safest thing to do, if there is the slightest
hesitation.

So the correct behaviour is:
"Please mister customer, what exactly did you have in mind?"

Groetjes Albert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,802
Messages
2,569,661
Members
45,428
Latest member
ViolettePa

Latest Threads

Top