Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

regex - Detect re (regexp) object in Python

I wonder what is the proper pythonic backward- and forward-compatible method how check if an object is compiled re object.

isinstance method cannot be easily used, while the resulting object claims to be _sre.SRE_Pattern object:

>>> import re
>>> rex = re.compile('')
>>> rex
<_sre.SRE_Pattern object at 0x7f63db414390>

but there is no such one:

>>> import _sre
>>> _sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

>>> import sre
__main__:1: DeprecationWarning: The sre module is deprecated, please import re.
>>> sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

>>> re.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'

I don't want to use duck typing (i.e. checking for the availability of some specific methods), because this could collide with some other types.

For now, I'm using:

>>> RegexpType = type(re.compile(''))
>>> type(rex) == RegexpType
True

but there might be a better way..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

re._pattern_type exists, and appears to do what you want:

>>> isinstance(re.compile(''), re._pattern_type)
True

But this is not a good idea - per Python convention, names starting with _ are not part of the public API of a module and not part of the backward compatibility guarantees. So, using type(re.compile('')) is your best bet - though notice that this isn't guaranteed to work either, since the re module makes no mention of the object returned from re.compile() being of any particular class.

And indeed, even if this was guaranteed, the most Pythonic and back- and forward- compatible way would be to rely on the interface, rather than the type. In other words, embracing duck typing and EAFP, do something like this:

try:
     rex.match(my_string)
except AttributeError:
     # rex is not an re
else:
     # rex is an re

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...