Subclassing built-in types

Discussion in 'Python' started by Emiliano Molina, Aug 25, 2004.

  1. 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.
    Emiliano Molina, Aug 25, 2004
    #1
    1. Advertising

  2. 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!)
    Emiliano Molina, Aug 25, 2004
    #2
    1. Advertising

  3. [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 Hindle, Aug 26, 2004
    #3
  4. Thank you, thats just what I needed.
    Emiliano Molina, Aug 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Cooper

    subclassing built-in types

    Gabriel Cooper, May 24, 2004, in forum: Python
    Replies:
    0
    Views:
    281
    Gabriel Cooper
    May 24, 2004
  2. Jane Austine
    Replies:
    12
    Views:
    424
    Nick Coghlan
    Feb 24, 2005
  3. MonkeeSage

    Subclassing built-in classes

    MonkeeSage, Oct 5, 2006, in forum: Python
    Replies:
    12
    Views:
    531
    Theerasak Photha
    Oct 11, 2006
  4. Alona
    Replies:
    2
    Views:
    633
  5. kj
    Replies:
    2
    Views:
    224
Loading...

Share This Page