[Python-ideas] Use p"" to represent `pattern_str` -- a subclass of `str`

Ma Lin malincns at 163.com
Sat Dec 29 06:49:41 EST 2018


I have a compromise idea, here is some points:

1, Create a built-in class `pattern_str` which is a subclass of `str`, 
it's dedicated to regex pattern string.
2, Use p"" to represent `pattern_str`.

Some advantages:

1, Since it's a subclass of `str`, we can use it as normal `str`.
2, IDE/linter/compiler can identify it as an regex pattern, something 
like type hint in language level.
3, We can still store compiled pattern in .pyc file *quietly*.
4, Won't introduce Perl style into Python, to avoid abusing regex in 
some degree.

We still using regex in the old way:

import re
re.search(p"(?i)[a-z]", s)

But if re.search() find the pattern is a `pattern_str`, it load compiled 
pattern from .pyc file directly.



More information about the Python-ideas mailing list