Comparing types
Terry Reedy
tjreedy at udel.edu
Sun Feb 17 04:42:20 EST 2013
On 2/17/2013 1:34 AM, Jason Friedman wrote:
> 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)
> True
>>>> my_pattern = re.compile(s)
>>>> type(my_pattern)
> <class '_sre.SRE_Pattern'>
The .__name__ attribute of the class is '_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
but the class is not bound to a (builtin) name, including not to
_sre.SRE_Pattern
--
Terry Jan Reedy
More information about the Python-list
mailing list