Python 中编译的正则表达式的类型是什么?
特别是,我想评估
isinstance(re.compile(''), ???)
是真的,为了自省的目的。
我有一个解决方案,有一些全局常数 REGEX_TYPE = type(re.compile(''))
,但它似乎不是很优雅。
编辑: 我想这样做的原因是因为我有字符串列表和已编译的正则表达式对象。我想“匹配”一个字符串与列表,通过
我想出来的代码是:
for allowed in alloweds:
if isinstance(allowed, basestring) and allowed == input:
ignored = False
break
elif isinstance(allowed, REGEX_TYPE) and allowed.match(input):
ignored = False
break