Comparing types

J

Jason Friedman

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.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_sre' is not defined
 
S

Steven D'Aprano

Jason said:
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.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_sre' is not defined


Here are two ways to do this. The first is not portable, and is not
guaranteed to work, since the _sre module is private.


# Risky but obvious.
import _sre
isinstance(my_pattern, _sre.SRE_Pattern)


The second is guaranteed to work even if the _sre module disappears, is
renamed, or otherwise changes in any way.


# Safe.
PatternType = type(re.compile("."))
isinstance(my_pattern, PatternType)
 
R

Rick Johnson

[...]
py> my_pattern = re.compile(s)
py> type(my_pattern)
<class '_sre.SRE_Pattern'>
py> isinstance(my_pattern, _sre.SRE_Pattern)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_sre' is not defined

Both Steven and Terry provided answers to you question however you not still understand why the line "isinstance(my_pattern, _sre.SRE_Pattern)" threw a NameError.

Maybe you expected '_sre.SRE_Pattern' to be imported as a consequence of importing the "re" module? Hmm, not so. And you can even check these things yourself by using the global function: "dir".

py> dir()
['__builtins__', '__doc__', '__name__', '__package__']
py> import re
py> dir()
['__builtins__', '__doc__', '__name__', '__package__', 're']

As you can see only "re" is available after import, and Python does not look /inside/ namespaces (except the __builtin__ namespace) to resolve names without a dotted path. But even /if/ Python /did/ look inside the "re" namespace, it would not find the a reference to the module named "_sre" anyway! Observe:

py> dir(re)
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
py> '_sre' in dir(re)
False

And if you look in the modules "_sre.py", "sre_parse.py", and "sre_constants.py you cannot find the symbol "SRE_Pattern" anywhere -- almost seems likemagic huh? Heck the only "_sre.py" file i could find was waaaay down here:

...\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp\_sre.py

What were they doing, trying to bury it deeper than Jimmy Hoffa? Maybe one of my fellow Pythonistas would like to explain that mess?
 
R

Rick Johnson

[...]
py> my_pattern = re.compile(s)
py> type(my_pattern)
<class '_sre.SRE_Pattern'>
py> isinstance(my_pattern, _sre.SRE_Pattern)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_sre' is not defined

Both Steven and Terry provided answers to you question however you not still understand why the line "isinstance(my_pattern, _sre.SRE_Pattern)" threw a NameError.

Maybe you expected '_sre.SRE_Pattern' to be imported as a consequence of importing the "re" module? Hmm, not so. And you can even check these things yourself by using the global function: "dir".

py> dir()
['__builtins__', '__doc__', '__name__', '__package__']
py> import re
py> dir()
['__builtins__', '__doc__', '__name__', '__package__', 're']

As you can see only "re" is available after import, and Python does not look /inside/ namespaces (except the __builtin__ namespace) to resolve names without a dotted path. But even /if/ Python /did/ look inside the "re" namespace, it would not find the a reference to the module named "_sre" anyway! Observe:

py> dir(re)
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
py> '_sre' in dir(re)
False

And if you look in the modules "_sre.py", "sre_parse.py", and "sre_constants.py you cannot find the symbol "SRE_Pattern" anywhere -- almost seems likemagic huh? Heck the only "_sre.py" file i could find was waaaay down here:

...\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp\_sre.py

What were they doing, trying to bury it deeper than Jimmy Hoffa? Maybe one of my fellow Pythonistas would like to explain that mess?
 
C

Chris Angelico

And if you look in the modules "_sre.py", "sre_parse.py", and "sre_constants.py you cannot find the symbol "SRE_Pattern" anywhere -- almost seems like magic huh? Heck the only "_sre.py" file i could find was waaaay down here:

...\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp\_sre.py

What were they doing, trying to bury it deeper than Jimmy Hoffa? Maybe one of my fellow Pythonistas would like to explain that mess?

I have ~/cpython/Modules/_src.c and .o in my build directory. I
suspect that that might be where it is.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top