Inheriting the str type
Andrew Bennetts
andrew-pythonlist at puzzling.org
Mon Feb 3 22:50:01 EST 2003
On Tue, Feb 04, 2003 at 04:23:24PM +1300, Mike McGavin wrote:
>
> Following up on my query 24 hours ago about CGI and database-friendly
> string manipulation modules, I've been experimenting with writing my
> own. So far I've made a class which acts in a similar way to python's
> str type with a few extras. I'd like to inherit the python 'str' type
> as a base class, but I've been unsuccessful because of a problem that
> I've encountered.
The str type is immutable, so to subclass it you probably need to use
__new__.
[..snip..]
>
> Without inheriting I can still make my class act almost the same as
> python's 'str' type, but I'd like to inherit it if possible. Otherwise
> any code that checks that a parameter is a 'str' type breaks, unless
> there's an explicit conversion made back to a python string.
In theory, Pythonic code won't care if something is a string, but simply if
it behaves more-or-less like a string. Inheritance isn't necessarily the
right way for a class to say "I implement this behaviour".
-Andrew.
More information about the Python-list
mailing list