[Python-ideas] Add regex pattern literal p""
stefan_ml at behnel.de
Thu Dec 27 09:42:17 EST 2018
Ma Lin schrieb am 27.12.18 um 14:15:
>> It'd be good to know just how much benefit this precompilation actually
> As far as I know, Pattern objects in regex module can be pickled, don't
> know if it's useful.
>>>> import pickle
>>>> import regex
That's from the external regex package, not the stdlib re module.
>>>> p = regex.compile('[a-z]')
>>>> b = pickle.dumps(p)
>>>> p = pickle.loads(b)
Look a little closer:
>>> import pickle, re
>>> p = re.compile("[abc]")
What this does, essentially, is to make the pickle loader pass the original
regex pattern string into re.compile() to "unpickle" it. Meaning, it
compiles the regex on the way in. Thus, there isn't much to gain from using
(the current form of) regex pickling here.
I'm not saying that this can't be changed, but personally, this is exactly
what I would do if I was asked to make a compiled regex picklable.
Everything else would probably get you into portability hell.
More information about the Python-ideas