subclassing Python types

Robert Kern robert.kern at gmail.com
Thu Aug 30 15:44:19 EDT 2007


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

class MyString(str):
    def __init__(self, strInput):
        str.__init__(self, strInput)
        # ... other stuff

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list