अधिकतर, इस बात में कोई अंतर नहीं है कि आप re.compile का उपयोग करते हैं या नहीं। आंतरिक रूप से, सभी कार्यों को एक संकलन कदम के रूप में कार्यान्वित किया जाता है:
def match(pattern, string, flags=0):
return _compile(pattern, flags).match(string)
def fullmatch(pattern, string, flags=0):
return _compile(pattern, flags).fullmatch(string)
def search(pattern, string, flags=0):
return _compile(pattern, flags).search(string)
def sub(pattern, repl, string, count=0, flags=0):
return _compile(pattern, flags).sub(repl, string, count)
def subn(pattern, repl, string, count=0, flags=0):
return _compile(pattern, flags).subn(repl, string, count)
def split(pattern, string, maxsplit=0, flags=0):
return _compile(pattern, flags).split(string, maxsplit)
def findall(pattern, string, flags=0):
return _compile(pattern, flags).findall(string)
def finditer(pattern, string, flags=0):
return _compile(pattern, flags).finditer(string)
इसके अलावा, re.compile () अतिरिक्त अप्रत्यक्ष और कैशिंग तर्क को दरकिनार करता है:
_cache = {}
_pattern_type = type(sre_compile.compile("", 0))
_MAXCACHE = 512
def _compile(pattern, flags):
# internal: compile pattern
try:
p, loc = _cache[type(pattern), pattern, flags]
if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
return p
except KeyError:
pass
if isinstance(pattern, _pattern_type):
if flags:
raise ValueError(
"cannot process flags argument with a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags)
if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE:
_cache.clear()
if p.flags & LOCALE:
if not _locale:
return p
loc = _locale.setlocale(_locale.LC_CTYPE)
else:
loc = None
_cache[type(pattern), pattern, flags] = p, loc
return p
Re.compile का उपयोग करने से होने वाली छोटी गति के लाभ के अलावा , लोग उस पठनीयता को भी पसंद करते हैं जो संभावित जटिल पैटर्न विनिर्देशों के नामकरण से आती है और जहां वे लागू होते हैं, उन्हें व्यावसायिक तर्क से अलग करती हैं:
#### Patterns ############################################################
number_pattern = re.compile(r'\d+(\.\d*)?') # Integer or decimal number
assign_pattern = re.compile(r':=') # Assignment operator
identifier_pattern = re.compile(r'[A-Za-z]+') # Identifiers
whitespace_pattern = re.compile(r'[\t ]+') # Spaces and tabs
#### Applications ########################################################
if whitespace_pattern.match(s): business_logic_rule_1()
if assign_pattern.match(s): business_logic_rule_2()
ध्यान दें, एक अन्य प्रतिवादी ने गलत तरीके से माना कि pyc फाइलें संकलित पैटर्न को सीधे संग्रहीत करती हैं; हालांकि, वास्तव में वे PYC लोड होने पर हर बार फिर से बनाए जाते हैं:
>>> from dis import dis
>>> with open('tmp.pyc', 'rb') as f:
f.read(8)
dis(marshal.load(f))
1 0 LOAD_CONST 0 (-1)
3 LOAD_CONST 1 (None)
6 IMPORT_NAME 0 (re)
9 STORE_NAME 0 (re)
3 12 LOAD_NAME 0 (re)
15 LOAD_ATTR 1 (compile)
18 LOAD_CONST 2 ('[aeiou]{2,5}')
21 CALL_FUNCTION 1
24 STORE_NAME 2 (lc_vowels)
27 LOAD_CONST 1 (None)
30 RETURN_VALUE
उपरोक्त disassembly PYC फ़ाइल से tmp.pyयुक्त होता है:
import re
lc_vowels = re.compile(r'[aeiou]{2,5}')
re.subएक झंडे का तर्क नहीं लगेगा ...