Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39014 > unrolled thread

Comparing types

Started byJason Friedman <jsf80238@gmail.com>
First post2013-02-16 23:34 -0700
Last post2013-02-17 14:13 -0800
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Comparing types Jason Friedman <jsf80238@gmail.com> - 2013-02-16 23:34 -0700
    Re: Comparing types Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-17 19:38 +1100
    Re: Comparing types Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-17 14:13 -0800
      Re: Comparing types Chris Angelico <rosuav@gmail.com> - 2013-02-18 17:50 +1100
    Re: Comparing types Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-17 14:13 -0800

#39014 — Comparing types

FromJason Friedman <jsf80238@gmail.com>
Date2013-02-16 23:34 -0700
SubjectComparing types
Message-ID<mailman.1881.1361082907.2939.python-list@python.org>
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'>
>>> isinstance(my_pattern, _sre.SRE_Pattern)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_sre' is not defined

[toc] | [next] | [standalone]


#39018

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-02-17 19:38 +1100
Message-ID<512096fb$0$30002$c3e8da3$5496439d@news.astraweb.com>
In reply to#39014
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'>
>>>> isinstance(my_pattern, _sre.SRE_Pattern)
> 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)




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#39042

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-17 14:13 -0800
Message-ID<ea2c363c-660a-4233-883e-47c021b2711d@googlegroups.com>
In reply to#39014
On Sunday, February 17, 2013 12:34:57 AM UTC-6, Jason Friedman wrote:
> [...]
> 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 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?

[toc] | [prev] | [next] | [standalone]


#39074

FromChris Angelico <rosuav@gmail.com>
Date2013-02-18 17:50 +1100
Message-ID<mailman.1928.1361170233.2939.python-list@python.org>
In reply to#39042
On Mon, Feb 18, 2013 at 9:13 AM, Rick Johnson
<rantingrickjohnson@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#39043

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-02-17 14:13 -0800
Message-ID<mailman.1907.1361139192.2939.python-list@python.org>
In reply to#39014
On Sunday, February 17, 2013 12:34:57 AM UTC-6, Jason Friedman wrote:
> [...]
> 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 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?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web