Possible Duplicate:
Check if a variable is SRE_Match
I need to determine if the parameter passed to a function is a regular expression match type or not. Currently I have:
re_type = type(re.compile(''))
def func(result):
if isinstance(result, re_type):
print("re")
However, I can never get it to print re. When I print result, I get:
<_sre.SRE_Match object at 0x7fed5330>
Is there an easier way to recognize this object?
Change first line to:
Currently you have the re_type set to the type of a regular expression, not the match that results from the application of a regular expression to input.
Note also that the re module will return None where there is no match, so if your function really only expects a match type or None, you might just do:
Lastly, you may be violating the EAFP principle of Python. This last version assumes result is a match type unless something breaks…