ishexdigit()

Tim Roberts timr at probo.com
Wed Dec 31 03:06:57 EST 2003


Ben Finney <bignose-hates-spam at and-benfinney-does-too.id.au> wrote:
>
>More Pythonish is to use string.hexdigits and map() to write your own:
>
>    >>> import string
>    >>> def ishexdigit( char ):
>    ...     result = ( char in string.hexdigits )
>    ...     return result
>    ...
>    >>> def ishexstring( str ):
>    ...     resultmap = map( ishexdigit, str )
>    ...     result = ( False not in resultmap )
>    ...     return result
>    ...
>    >>> ishexstring( '010101' )
>    True
>    >>> ishexstring( 'deadb00f' )
>    True
>    >>> ishexstring( 'foob' )
>    False

Well, more Pythonish yet is to avoid the extraneous local variables, avoid
the use of a type as a parameter name, and use as many little-used builtins
as possible:

   def ishexdigit(char):
       return char in string.hexdigits

   def ishexstring(strng):
       return filter(ishexdigit, strng) == strng

-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.




More information about the Python-list mailing list