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


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

Type object returned by the re.compile function

Started bycandide <candide@free.invalid>
First post2011-12-27 22:56 +0100
Last post2011-12-27 15:31 -0700
Articles 3 — 3 participants

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


Contents

  Type object returned by the re.compile function candide <candide@free.invalid> - 2011-12-27 22:56 +0100
    Re: Type object returned by the re.compile function Jerry Hill <malaclypse2@gmail.com> - 2011-12-27 17:29 -0500
    Re: Type object returned by the re.compile function Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-27 15:31 -0700

#18063 — Type object returned by the re.compile function

Fromcandide <candide@free.invalid>
Date2011-12-27 22:56 +0100
SubjectType object returned by the re.compile function
Message-ID<4efa3f05$0$6546$426a74cc@news.free.fr>
The Python 2.7 official documentation here:

http://docs.python.org/library/re.html#re.compile

doesn't specify the type object returned by the re.compiled function. 
According to the documentation, re.compile returns a "regular expression 
object".

A regular expression object seems to be an instance of the class 
RegexObject. The documentation mentions this class here :

http://docs.python.org/library/re.html#regular-expression-objects

but i don't see any existing class with this name :

 >>> import re
 >>> re.RegexObject
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'RegexObject'
 >>>

Actually, a regular expression object doesn't seem to be a class object :

 >>> import re
 >>> reo = re.compile('')
 >>> reo.__class__
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: __class__
 >>>

but has attributes :

 >>> dir(reo)
['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner', 
'search', 'split', 'sub', 'subn']
 >>>


I have the same problem with the match object : the doc mentions a 
re.MatchObject class but this class doesn't exist :

 >>> re.MatchObject
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MatchObject'
 >>>


Any clarification is welcome.

[toc] | [next] | [standalone]


#18067

FromJerry Hill <malaclypse2@gmail.com>
Date2011-12-27 17:29 -0500
Message-ID<mailman.4158.1325024967.27778.python-list@python.org>
In reply to#18063
On Tue, Dec 27, 2011 at 4:56 PM, candide <candide@free.invalid> wrote:
>>>> import re
>>>> reo = re.compile('')
>>>> reo.__class__
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> AttributeError: __class__
>>>>

I'm not going to comment on what type is returned from the various
functions in the re module, mostly because all I have installed
locally is 2.6, and I'm not sure if anything has changed in 2.7 or
3.2.

Instead, I will recommend a different tool for your  toolbox.  Take a
look at the type() builtin function (
http://docs.python.org/library/functions.html#type ).  Particularly,
instead of inspecting reo.__class__ in your example above, you can
print out type(reo).

At a guess, those objects are missing the __class__ attribute,
possibly because they are old style classes.

-- 
Jerry

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


#18068

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-12-27 15:31 -0700
Message-ID<mailman.4159.1325025123.27778.python-list@python.org>
In reply to#18063
On Tue, Dec 27, 2011 at 2:56 PM, candide <candide@free.invalid> wrote:
> The Python 2.7 official documentation here:
>
> http://docs.python.org/library/re.html#re.compile
>
> doesn't specify the type object returned by the re.compiled function.
> According to the documentation, re.compile returns a "regular expression
> object".
>
> A regular expression object seems to be an instance of the class
> RegexObject. The documentation mentions this class here :
>
> http://docs.python.org/library/re.html#regular-expression-objects
>
> but i don't see any existing class with this name :

Presumably if it's not in the module then it's not meant to be
directly invoked.  You should always use re.compile().  If you really
want to assign the type a name, though:

>>> import re
>>> RegexObject = type(re.compile(''))
>>> RegexObject
<type '_sre.SRE_Pattern'>

Cheers,
Ian

[toc] | [prev] | [standalone]


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


csiph-web