Inheriting the str type

Mike C. Fletcher mcfletch at rogers.com
Mon Feb 3 22:39:01 EST 2003


 >>> class s( str ):
...     def __new__( cls, a, b ):
...         obj = super( s, cls).__new__( s, a )
...         obj.b = b
...         return obj
...
 >>> s( 'this', 'that' )
'this'
 >>> s( 'this', 'that' ).b
'that'
 >>>

I have no idea if that's considered the proper way to do the 
initialisation, but it does appear to work.

Good luck,
Mike

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.

...

> >>> class zstr(str):
> ...     def __init__(self, value, arg2=None):
> ...             str.__init__(self, value)
> ...             self.__arg2 = arg2
> ...
> >>> a = zstr("String value", "second argument")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: str() takes at most 1 argument (2 given)

...

 
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list