Subclassing built-in types

E

Emiliano Molina

Where can I find information on how to do this?

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

Where can I find that documentation? I've looked through the manual but
can't find anything relevant. Google searches bring up articles that
talk about code as described in my first example but I can't find
anything like the second.
 
E

Emiliano Molina

Emiliano Molina wrote:
--- some stuff I was confused about ---
Where can I find that documentation? I've looked through the manual but
can't find anything relevant. Google searches bring up articles that
talk about code as described in my first example but I can't find
anything like the second.

I received no answers to my post (not complaining) and I am curious as
to wether it was a silly question or if there was no answer because what
I wanted to know is not documented.

Thanks for your replies (if there are going to be any!)
 
R

Richie Hindle

[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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top