What is the type of the compiled regular expression in python?
In particular, I want to evaluate
isinstance(re.compile(''), ???)
to be true, for introspection purposes.
One solution I had was, have some global constant REGEX_TYPE = type(re.compile(''))
, but it doesn't seem very elegant.
EDIT: The reason I want to do this is because I have list of strings and compiled regex objects. I want to "match" a string against list, by
and the code that I came up with was:
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
When the type of something isn't well specified, there's nothing wrong with using the type
builtin to discover the answer at runtime:
>>> import re
>>> retype = type(re.compile('hello, world'))
>>> isinstance(re.compile('goodbye'), retype)
True
>>> isinstance(12, retype)
False
>>>
Discovering the type at runtime protects you from having to access private attributes and against future changes to the return type. There's nothing inelegant about using type
here, though there may be something inelegant about wanting to know the type at all.
That said, with the passage of time, the context of this question has shifted. With contemporary versions of Python, the return type of re.compile
is now re.Pattern
.
The general question about what to do if the type of something is not well-specified is still valid but in this particular case, the type of re.compile(...)
is now well-specified.