How to access the help page of SRE_Pattern?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Mar 5 20:01:42 EST 2018


On Mon, 05 Mar 2018 18:13:59 -0600, Peng Yu wrote:

> Hi,
> 
>>>> import re
>>>> prog=re.compile('[a-f]+')
>>>> help(prog)
> 
> I can use the above command to access SRE_Pattern. But this involves the
> creation of an object of the class.

If you're using help() interactively, the cost of creating the instance 
is about a millionth of the cost of actually reading the help text.

help(re.compile(''))

should be perfectly acceptable, performance-wise, even though it is a 
tiny bit longer to write than:

help(re)


If you really want a reference to the SRE_Pattern class, I'm afraid it is 
not public name. It is technically an implementation detail subject to 
change without notice. It could change its name, its internal details, 
its location, so long as it still offers the public regular expression 
interface.

I believe that SRE_Pattern is a built-in class, literally built into the 
interpreter.

The best way to get access to it is to do this once, at the beginning of 
your code:

SRE_Pattern = type(re.compile(''))


If you are doing this in the interactive interpreter, you might want to 
include that in your Python startup file.



-- 
Steve




More information about the Python-list mailing list