Overriding list.__new__

Discussion in 'Python' started by Michele Simionato, Jul 3, 2003.

  1. Let me show first how does it work for tuples:

    >>> class MyTuple(tuple):

    .... def __new__(cls,strng): # implicit conversion string of ints => tuple
    .... return super(MyTuple,cls).__new__(cls,map(int,strng.split()))
    >>> MyTuple('1 2')

    (1, 2)

    No wonder here, everything is fine. However, if I do the same for
    lists I get the following:

    >>> class MyList(list):

    .... def __new__(cls,strng): #implicit conversion string of ints => tuple
    .... return super(MyList,cls).__new__(cls,map(int,strng.split()))
    >>> MyList('1 2')

    ['1', ' ', '2']

    The same is true for

    >>> class MyList(list):

    .... def __new__(cls,strng):
    .... return list.__new__(cls,map(int,strng.split()))
    >>> MyList('1 2')

    ['1', ' ', '2']

    therefore it is not a problem of super.
    The 'map' expression does not seem to be executed or, if its executed,
    it has no effect at all. If I replace 'map' with anything, still I have
    the same result:

    >>> class MyList(list):

    .... def __new__(cls,strng):
    .... return list.__new__(cls,map(int,[]) # !notice: empty list here!
    >>> MyList('1 2')

    ['1', ' ', '2']

    In other words I always get the result of

    >>> list('1 2')

    ['1', ' ', '2']

    and it seems impossible to override list.__new__.

    I am very puzzled about that; any suggestions?

    Michele
    Michele Simionato, Jul 3, 2003
    #1
    1. Advertising

  2. Michele Simionato

    Terry Reedy Guest

    "Michele Simionato" <> wrote in message
    news:...
    > Let me show first how does it work for tuples:
    >
    > >>> class MyTuple(tuple):

    > ... def __new__(cls,strng): # implicit conversion string of ints

    => tuple
    > ... return

    super(MyTuple,cls).__new__(cls,map(int,strng.split()))
    > >>> MyTuple('1 2')

    > (1, 2)
    >
    > No wonder here, everything is fine. However, if I do the same for
    > lists I get the following:


    Values of immutable objects must be set when created, because they
    cannot be changed thereafter. Mutable objects can be initialized in
    the __init__() method. I suspect this is true of lists, so that
    overriding __new__ for lists has no effect.

    TJR
    Terry Reedy, Jul 3, 2003
    #2
    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. chenyu
    Replies:
    1
    Views:
    734
    Alex Martelli
    Nov 7, 2003
  2. Christoph Groth

    Overriding the __new__ method

    Christoph Groth, Feb 8, 2004, in forum: Python
    Replies:
    3
    Views:
    310
    Gerrit
    Feb 8, 2004
  3. Steven Bethard
    Replies:
    2
    Views:
    447
    Steven Bethard
    Feb 16, 2005
  4. Ken Schutte

    modifying __new__ of list subclass

    Ken Schutte, Aug 15, 2006, in forum: Python
    Replies:
    5
    Views:
    318
    Ken Schutte
    Aug 15, 2006
  5. macaronikazoo

    __new__ woes with list

    macaronikazoo, Nov 21, 2008, in forum: Python
    Replies:
    3
    Views:
    299
    Arnaud Delobelle
    Nov 21, 2008
Loading...

Share This Page