Does Python optimize regexes?

Jason Smith jhs at oes.co.th
Tue Jun 29 23:28:47 EDT 2004


Thanks much to Peter and Michael for the clarification.

Peter Otten wrote:
> So not explicitly calling compile() in advance only costs you two function
> calls and a dictionary lookup - and maybe some clarity in your code.

The reason I asked is because I felt that re.compile() was less clear:

someRegex = re.compile('searchforme')
while something:
	theString = getTheString()
	if someRegex.search(theString):
		celebrate()

I wanted to remove someRegex since I can shave a line of code and some 
confusion, but I was worried about re.search() in a loop.

The answer is this is smartly handled in Python, as opposed to bytecode 
optimizations.  Great!

-- 
Jason Smith
Open Enterprise Systems
Bangkok, Thailand
http://oes.co.th
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 264 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20040630/46e30d01/attachment.sig>


More information about the Python-list mailing list