Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do both the sre and re modules exist in Python?

Tags:

python

regex

I have always assumed that when I saw people mention the sre module, it was a typo and they actually meant re.

Today, I accidentally typed import sre into the interpreter and was surprised that it worked. I decided to dig a bit more into what the sre module was via help(sre) and... everything about what it does is the exact same as re.

Why do both modules exist? Which came first? Why was the second created? Is one getting deprecated and/or removed (is it already gone in Python 3? I don't have a Python 3 interpreter on this computer to check with.)

like image 672
ArtOfWarfare Avatar asked Sep 15 '15 01:09

ArtOfWarfare


1 Answers

Yes, it is deprecated. When you did help(sre), you must have seen this line:

DESCRIPTION
    This file is only retained for backwards compatibility.
    It will be removed in the future.  sre was moved to re in version 2.5.

So it's used for code that is designed for Python <2.5, but it is now replaced by re.

Furthermore, it was removed:

# Python 3
>>> import sre
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'sre'
like image 74
TerryA Avatar answered Sep 17 '22 20:09

TerryA