[Python-ideas] Add regex pattern literal p""

Chris Angelico rosuav at gmail.com
Thu Dec 27 12:27:46 EST 2018


On Fri, Dec 28, 2018 at 12:15 AM Ma Lin <malincns at 163.com> wrote:
>
>  > It'd be good to know just how much benefit this precompilation
> actually grants.
>
> As far as I know, Pattern objects in regex module can be pickled, don't
> know if it's useful.
>
>  >>> import pickle
>  >>> import regex
>  >>> p = regex.compile('[a-z]')
>  >>> b = pickle.dumps(p)
>  >>> p = pickle.loads(b)

What Stefan pointed out regarding the stdlib's "re" module is also
true of the third party "regex" - unpickling just compiles from the
original string.

Regarding pyc files, though, pickle is less significant than marshal.
And both re.compile() and regex.compile() return unmarshallable
objects. Fortunately, marshal doesn't need to produce cross-compatible
files, so the portability issues don't apply.

So, let's suppose that marshalling a compiled regex became possible.
It would need to be (a) absolutely guaranteed to have the same effect
as compiling the original text string, and (b) faster than compiling
the original text string, otherwise it's useless. This is where
testing would be needed: can it actually save any significant amount
of time?

>  > Wow that's an old post of mine
> I searched on Google before post this, hope there is no omission.

You're absolutely fine :) I was amused to find that a post of mine
from nearly six years ago should be the most notable on the subject,
is all. Good work digging it up.

ChrisA


More information about the Python-ideas mailing list