Overriding list.__new__

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele Simionato

    Overriding list.__new__

    Let me show first how does it work for tuples:
    [color=blue][color=green][color=darkred]
    >>> class MyTuple(tuple):[/color][/color][/color]
    .... def __new__(cls,str ng): # implicit conversion string of ints => tuple
    .... return super(MyTuple,c ls).__new__(cls ,map(int,strng. split()))[color=blue][color=green][color=darkred]
    >>> MyTuple('1 2')[/color][/color][/color]
    (1, 2)

    No wonder here, everything is fine. However, if I do the same for
    lists I get the following:
    [color=blue][color=green][color=darkred]
    >>> class MyList(list):[/color][/color][/color]
    .... def __new__(cls,str ng): #implicit conversion string of ints => tuple
    .... return super(MyList,cl s).__new__(cls, map(int,strng.s plit()))[color=blue][color=green][color=darkred]
    >>> MyList('1 2')[/color][/color][/color]
    ['1', ' ', '2']

    The same is true for
    [color=blue][color=green][color=darkred]
    >>> class MyList(list):[/color][/color][/color]
    .... def __new__(cls,str ng):
    .... return list.__new__(cl s,map(int,strng .split()))[color=blue][color=green][color=darkred]
    >>> MyList('1 2')[/color][/color][/color]
    ['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:
    [color=blue][color=green][color=darkred]
    >>> class MyList(list):[/color][/color][/color]
    .... def __new__(cls,str ng):
    .... return list.__new__(cl s,map(int,[]) # !notice: empty list here![color=blue][color=green][color=darkred]
    >>> MyList('1 2')[/color][/color][/color]
    ['1', ' ', '2']

    In other words I always get the result of
    [color=blue][color=green][color=darkred]
    >>> list('1 2')[/color][/color][/color]
    ['1', ' ', '2']

    and it seems impossible to override list.__new__.

    I am very puzzled about that; any suggestions?

    Michele
  • Terry Reedy

    #2
    Re: Overriding list.__new__


    "Michele Simionato" <mis6@pitt.ed u> wrote in message
    news:2259b0e2.0 307031246.60546 93d@posting.goo gle.com...[color=blue]
    > Let me show first how does it work for tuples:
    >[color=green][color=darkred]
    > >>> class MyTuple(tuple):[/color][/color]
    > ... def __new__(cls,str ng): # implicit conversion string of ints[/color]
    => tuple[color=blue]
    > ... return[/color]
    super(MyTuple,c ls).__new__(cls ,map(int,strng. split()))[color=blue][color=green][color=darkred]
    > >>> MyTuple('1 2')[/color][/color]
    > (1, 2)
    >
    > No wonder here, everything is fine. However, if I do the same for
    > lists I get the following:[/color]

    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


    Comment

    Working...