Type object returned by the re.compile function

C

candide

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 :
Traceback (most recent call last):

Actually, a regular expression object doesn't seem to be a class object :
Traceback (most recent call last):

but has attributes :
['__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 :
Traceback (most recent call last):


Any clarification is welcome.
 
J

Jerry Hill

Traceback (most recent call last):

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.
 
I

Ian Kelly

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:
<type '_sre.SRE_Pattern'>

Cheers,
Ian
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top