On Dec 22, 9:53 pm, William McBrine <[EMAIL PROTECTED]> wrote: > Hi all, > > I'm pretty new to Python (a little over a month). I was wondering -- is > something like this: > > s = re.compile('whatever') > > def t(whatnot): > return s.search(whatnot) > > for i in xrange(1000): > print t(something[i]) > > significantly faster than something like this: > > def t(whatnot): > s = re.compile('whatever') > return s.search(whatnot) > > for i in xrange(1000): > result = t(something[i]) > > ?
No. Or is Python clever enough to see that the value of s will be the same > on every call, No. It doesn't have a crystal ball. > and thus only compile it once? But it is smart enough to maintain a cache, which achieves the desired result. Why don't you do some timings? While you're at it, try this: def t2(whatnot): return re.search('whatever', whatnot) and this: t3 = re.compile('whatever').search HTH, John -- http://mail.python.org/mailman/listinfo/python-list