Comparing types

Jason Friedman jsf80238 at
Sun Feb 17 07:34:57 CET 2013

I want to tell whether an object is a regular expression pattern.

Python 3.2.3 (default, Oct 19 2012, 20:10:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> s = "hello"
>>> type(s)
<class 'str'>
>>> isinstance(s, str)
>>> my_pattern = re.compile(s)
>>> type(my_pattern)
<class '_sre.SRE_Pattern'>
>>> isinstance(my_pattern, _sre.SRE_Pattern)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_sre' is not defined

More information about the Python-list mailing list