You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can often find the module re in the standard library modules but it can be replaced (if it is possible). I don't suggest removing it everywhere, there are places where its use is appropriate, but there are also places where it is an unnecessary solution and leads to unpleasant consequences (they can be found below)
Cons of regular expressions and reasons to replace regular expressions with functions and methods:
We spend time to compile re pattern (one time, but anyway we spend it)
In most cases simple string methods are faster (according to my benchmarks about 2x)
We can remove import re which will affect import time
Additionally: I think for those who don't know regular expressions, the code is more difficult to read and therefore difficult to maintain.
We can often find the module
rein the standard library modules but it can be replaced (if it is possible). I don't suggest removing it everywhere, there are places where its use is appropriate, but there are also places where it is an unnecessary solution and leads to unpleasant consequences (they can be found below)Cons of regular expressions and reasons to replace regular expressions with functions and methods:
import rewhich will affect import timeImportant
For those who want to work on the issue, please:
pyperf,hyperfine, andtunatogether with-X importtimeto compare import times and execution time.gh-130167: Improve speed of `module.function` by replacing `re`Linked PRs
difflib.IS_LINE_JUNKby replacingre#130170inspect.formatannotationby replacingre#130242ftplib.parse150by replacingre#130243textwrap.dedent()#131919textwrap.dedent()optimization #131925textwrap.{de,in}dent#131924_pydecimal._all_zerosand_pydecimal._exact_halfby replacingre#132065textwrap.dedent#132666textwrap.{de,in}dent(GH-131924) #143292