Problem Inheriting from "str" Class

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jun 13 02:28:25 EDT 2007


En Wed, 13 Jun 2007 03:01:16 -0300, bcwhite at pobox.com <bcwhite at pobox.com>  
escribió:

> I'm writing a program and want to create a class that is derived from
> the "str" base type.  When I do so, however, I have problems with the
> __init__ method.  When I run the code below, it will call my new
> __init__ method when there is zero or one (value) parameter.  However,
> if I try to pass two parameters or a named parameter, then it dies
> with an error indicating that it's actually trying to call the
> "str:__init__" method instead.

__init__ is rather useless for immutable types. You have to override  
__new__ instead.
See <http://docs.python.org/ref/customization.html>

-- 
Gabriel Genellina




More information about the Python-list mailing list