467,146 Members | 1,009 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Overriding list.__new__

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
Jul 18 '05 #1
  • viewed: 2085
Share:
1 Reply

"Michele Simionato" <mi**@pitt.edu> wrote in message
news:22**************************@posting.google.c om...
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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Simon Burton | last post: by
3 posts views Thread by Christoph Groth | last post: by
10 posts views Thread by Stuart McGraw | last post: by
1 post views Thread by Adonis | last post: by
5 posts views Thread by Ken Schutte | last post: by
6 posts views Thread by c james | last post: by
9 posts views Thread by mrstevegross | last post: by
reply views Thread by Ethan Furman | last post: by
3 posts views Thread by macaronikazoo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.