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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…