NEWBIE: ishexdigit revisited

Paul Rubin http
Mon Dec 29 19:41:50 EST 2003


engsolnom at ipns.com writes:
> >     def ishexnumber(sx):
> >         import re
> >         if not re.match('[0123456789abcdefABCDEF]*$', sx): return 0
> >         if len(sx) % 2 == 0: return 1
> >         return 'Extra nibble'
> If I run this many times, as is likely in our application, does the
> 'import re' chew up memory?

Importing the re module does use some memory, but it's the same amount
of memory whether you call the function once or many times.  Normally
you'd put 'import re' at the top of the file that the function is
defined in, by the way, rather than inside the function, but either
way works.

> Our strings will almost always be long ones, and the byte values will
> range from zero to 255, but I'll tuck this nugget into the archive.

This sounds like you almost certainly want to use the binascii module
and not write your own function.




More information about the Python-list mailing list