Subclassing built-in types

Richie Hindle richie at entrian.com
Thu Aug 26 04:06:59 EDT 2004


[Emiliano]
> I am concerned about something like:
> 
> class NewList(list):
> 	def __init__(self):
> 		list.__init__(self)
> 
> The above code does not allow the passing of a sequence to populate the 
> NewList.
> 
> I imagine that the correct way to do it is something like:
> 
> class NewList(list):
> 	def __init__(self,seq):
> 		list.__init__(self,seq)
> 
> but I can't find any documentation that describes what arguments 
> list.__init__ may take.

The safest way to do this is:

class NewList(list):
    def __init__(self, *args, **kwargs):
        list.__init__(self, *args, **kwargs)

That will work whatever arguments list.__init__ expects, and will continue
to work even if it changes in the future.  It's useful when subclassing
anything that isn't under your control.

(In the trivial case above, where your __init__ doesn't do anything, you can
 always just omit it.)

-- 
Richie Hindle
richie at entrian.com




More information about the Python-list mailing list