Subclassing RegexObject

T

Tobias Weber

Hi,
how do I subclass or at least add a method to something returned by
re.compile()?
 
P

Peter Otten

Tobias said:
how do I subclass or at least add a method to something returned by
re.compile()?

Let's see:
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type '_sre.SRE_Pattern' is not an acceptable base type

So I'm afraid you can't subclass...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type
'_sre.SRE_Pattern'Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_sre.SRE_Pattern' object has no attribute 'hello'

....nor add a method...
.... def __init__(self, match):
.... self._match = match
.... def __getattr__(self, name):
.... return getattr(self._match, name)
.... hello = hello
........ m = original_compile(*args, **kw)
.... if m is not None:
.... return W(m)
....hello

....but alter the meaning of "something returned by re.compile()" you can.
Now who would want to do that? And why?

Peter
 
T

Tobias Weber

[/QUOTE]

Thanks for showing the introspection way!
type '_sre.SRE_Pattern' is not an acceptable base type

And again the Python documentation only writes about how things would be
if they were actually implemented in Python...
Now who would want to do that? And why?

My class only really needs one attribute, which is a regular expression,
so I thought to save a line or two by subclassing that ;)
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top