在Python中检测re(regexp)对象 [英] Detect re (regexp) object in Python

查看:44
本文介绍了在Python中检测re(regexp)对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是正确的 Pythonic 向后和向前兼容的方法,如何检查对象是否被编译为 re 对象.

isinstance 方法不容易使用,而生成的对象声称是 _sre.SRE_Pattern 对象:

<预><代码>>>>进口重新>>>rex = re.compile('')>>>雷克斯<_sre.SRE_Pattern 对象在 0x7f63db414390>

但没有这样的:

<预><代码>>>>导入_sre>>>_sre.SRE_PatternAttributeError: 'module' 对象没有属性 'SRE_Pattern'>>>导入资源__main__:1: DeprecationWarning: 不推荐使用 sre 模块,请导入 re.>>>sre.SRE_PatternAttributeError: 'module' 对象没有属性 'SRE_Pattern'>>>re.SRE_PatternAttributeError: 'module' 对象没有属性 'SRE_Pattern'

我不想使用鸭子类型(即检查某些特定方法的可用性),因为这可能会与其他一些类型发生冲突.

目前,我正在使用:

<预><代码>>>>RegexpType = type(re.compile(''))>>>type(rex) == RegexpType真的

但可能有更好的方法..

解决方案

re._pattern_type 存在,并且似乎可以执行您想要的操作:

<预><代码>>>>isinstance(re.compile(''), re._pattern_type)真的

但这不是一个好主意 - 根据 Python 约定,以 _ 开头的名称不是模块公共 API 的一部分,也不是向后兼容性保证的一部分.所以,使用 type(re.compile('')) 是你最好的选择 - 但请注意,这也不能保证工作,因为 re 模块没有提到从 re 返回的对象.compile() 属于任何特定类.

事实上,即使这是有保证的,最 Pythonic 和向后和向前兼容的方式将依赖于接口,而不是类型.换句话说,拥抱鸭子打字和 EAFP,做这样的事情:

尝试:rex.match(my_string)除了属性错误:# rex 不是 re别的:# rex 是一个 re

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..

解决方案

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

这篇关于在Python中检测re(regexp)对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆