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


Groups > comp.lang.python > #39042

Re: Comparing types

X-Received by 10.180.98.102 with SMTP id eh6mr2463755wib.7.1361139188533; Sun, 17 Feb 2013 14:13:08 -0800 (PST)
X-Received by 10.49.96.196 with SMTP id du4mr519651qeb.37.1361139188095; Sun, 17 Feb 2013 14:13:08 -0800 (PST)
Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!209.85.212.216.MISMATCH!19no3112616wij.1!news-out.google.com!bp2ni41265wib.1!nntp.google.com!yu2no3604540wib.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Sun, 17 Feb 2013 14:13:07 -0800 (PST)
In-Reply-To <mailman.1881.1361082907.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=70.196.64.154; posting-account=h3aEwQoAAACiuqX-oR3gvCVFm8lLHoWj
NNTP-Posting-Host 70.196.64.154
References <mailman.1881.1361082907.2939.python-list@python.org>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <ea2c363c-660a-4233-883e-47c021b2711d@googlegroups.com> (permalink)
Subject Re: Comparing types
From Rick Johnson <rantingrickjohnson@gmail.com>
Cc python-list@python.org
Injection-Date Sun, 17 Feb 2013 22:13:08 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:39042

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web