{"id":1477,"date":"2022-08-18T19:28:01","date_gmt":"2022-08-18T19:28:01","guid":{"rendered":"http:\/\/www.wellformedness.com\/blog\/?p=1477"},"modified":"2022-08-18T19:28:01","modified_gmt":"2022-08-18T19:28:01","slug":"re-compile-otiose","status":"publish","type":"post","link":"https:\/\/www.wellformedness.com\/blog\/re-compile-otiose\/","title":{"rendered":"re.compile is otiose"},"content":{"rendered":"<p>Unlike its cousins Perl and Ruby, Python has no literal syntax for regular expressions. Whereas one can express the sheep language <code>\/baa+\/<\/code> with a simple forward-slashed literal in Perl and Ruby, in Python one has to compile them using the function <code>re.compile<\/code>, which produces objects of type <code>re.Pattern<\/code>. Such objects have various methods for string matching.<\/p>\n<pre>sheep = re.compile(r\"baa+\")\r\nassert sheep.match(\"baaaaaaaa\")\r\n<\/pre>\n<p>Except, one doesn&#8217;t actually have to compile regular expressions at all, as <a href=\"https:\/\/docs.python.org\/3\/library\/re.html#re.compile\">the documentation<\/a> explains:<\/p>\n<blockquote><p>Note: The compiled versions of the most recent patterns passed to <code>re.compile()<\/code> and the module-level matching functions are cached, so programs that use only a few regular expressions at a time needn\u2019t worry about compiling regular expressions.<\/p><\/blockquote>\n<p>What this means is that in the vast majority of cases, <code>re.compile<\/code> is otiose (i.e., unnecessary). One can just define expression strings, and pass them to the equivalent module-level functions rather than using the methods of <code>re.Pattern<\/code> objects.<\/p>\n<pre>sheep = r\"baa+\"\r\nassert re.match(sheep, \"baaaaaaaa\")\r\n<\/pre>\n<p>This, I would argue, is slightly easier to read, and certainly no slower. It also makes typing a bit more convenient since <code>str<\/code> is easier to type than <code>re.Pattern<\/code>.<\/p>\n<p>Now, I am sure there is <em>some<\/em> usage pattern which would favor explicit <code>re.compile<\/code>, but I have not encountered one in code worth profiling.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike its cousins Perl and Ruby, Python has no literal syntax for regular expressions. Whereas one can express the sheep language \/baa+\/ with a simple forward-slashed literal in Perl and Ruby, in Python one has to compile them using the function re.compile, which produces objects of type re.Pattern. Such objects have various methods for string &hellip; <a href=\"https:\/\/www.wellformedness.com\/blog\/re-compile-otiose\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;re.compile is otiose&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[3,8],"tags":[],"class_list":["post-1477","post","type-post","status-publish","format-standard","hentry","category-dev","category-python"],"_links":{"self":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/comments?post=1477"}],"version-history":[{"count":2,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1477\/revisions"}],"predecessor-version":[{"id":1479,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/posts\/1477\/revisions\/1479"}],"wp:attachment":[{"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/media?parent=1477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/categories?post=1477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellformedness.com\/blog\/wp-json\/wp\/v2\/tags?post=1477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}