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
[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
Comment