__new__ woes with list

Discussion in 'Python' started by macaronikazoo, Nov 21, 2008.

  1. i'm having a hell of a time getting this to work. basically I want to
    be able to instantiate an object using either a list, or a string, but
    the class inherits from list.

    if the class is instantiated with a string, then run a method over it
    to tokenize it in a meaningful way.

    so how come this doesn't work??? if I do this:

    a=TMP( 'some string' )

    it does nothing more than list('some string') and seems to be ignoring
    the custom __new__ method.



    def convertDataToList( data ): return [1,2,3]
    class TMP(list):
    def __new__( cls, data ):
    if isinstance(data, basestring):
    new = convertDataToList( data )
    return list.__new__( cls, new )

    if isinstance(data, list):
    return list.__new__( cls, data )
    macaronikazoo, Nov 21, 2008
    #1
    1. Advertising

  2. macaronikazoo <> writes:

    > i'm having a hell of a time getting this to work. basically I want to
    > be able to instantiate an object using either a list, or a string, but
    > the class inherits from list.
    >
    > if the class is instantiated with a string, then run a method over it
    > to tokenize it in a meaningful way.
    >
    > so how come this doesn't work??? if I do this:
    >
    > a=TMP( 'some string' )
    >
    > it does nothing more than list('some string') and seems to be ignoring
    > the custom __new__ method.
    >
    >
    >
    > def convertDataToList( data ): return [1,2,3]
    > class TMP(list):
    > def __new__( cls, data ):
    > if isinstance(data, basestring):
    > new = convertDataToList( data )
    > return list.__new__( cls, new )
    >
    > if isinstance(data, list):
    > return list.__new__( cls, data )


    A list is mutable, its initialisation is done in __init__() not
    __new__(). There was a recent post about this (in the last couple of
    weeks).

    --
    Arnaud
    Arnaud Delobelle, Nov 21, 2008
    #2
    1. Advertising

  3. ok thansk - i will search again. i did try searching but didn't find
    anything relevant...
    macaronikazoo, Nov 21, 2008
    #3
  4. Arnaud Delobelle, Nov 21, 2008
    #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. Michele Simionato

    Overriding list.__new__

    Michele Simionato, Jul 3, 2003, in forum: Python
    Replies:
    1
    Views:
    318
    Terry Reedy
    Jul 3, 2003
  2. chenyu
    Replies:
    1
    Views:
    731
    Alex Martelli
    Nov 7, 2003
  3. Jp Calderone
    Replies:
    2
    Views:
    310
    Alex Martelli
    Nov 10, 2003
  4. Steven Bethard
    Replies:
    2
    Views:
    444
    Steven Bethard
    Feb 16, 2005
  5. Ken Schutte

    modifying __new__ of list subclass

    Ken Schutte, Aug 15, 2006, in forum: Python
    Replies:
    5
    Views:
    317
    Ken Schutte
    Aug 15, 2006
Loading...

Share This Page