subclassing Python types

Arnaud Delobelle arnodel at googlemail.com
Thu Aug 30 21:52:59 CEST 2007


On Aug 30, 8:44 pm, Robert Kern <robert.k... at gmail.com> wrote:
> zzbba... at aol.com wrote:
> > So it's:
> > class MyString(str):
> >    def __init__(self,strInput):
> >       self = strInput
>
> That doesn't quite work. Assigning to "self" only reassigns the name inside the
> function. It does not replace the object.
>
> Instead, call the .__init__() method on str.

That won't do much as strings are immutable objects...
As pointed out by Steve Holden and me, str.__new__ is the way.
--
Arnaud





More information about the Python-list mailing list