467,877 Members | 1,291 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

__new__ woes with list

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 )
Nov 21 '08 #1
  • viewed: 1236
Share:
3 Replies
macaronikazoo <ma***********@gmail.comwrites:
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
Nov 21 '08 #2


ok thansk - i will search again. i did try searching but didn't find
anything relevant...
Nov 21 '08 #3
macaronikazoo <ma***********@gmail.comwrites:
ok thansk - i will search again. i did try searching but didn't find
anything relevant...
Here's a link to the thread on google groups:

http://groups.google.com/group/comp....ff4eabc6182858

Unfortunately two threads seem to be intertwined: scroll down to when
the thread is renamed "python bug when subclassing list?".

HTH

--
Arnaud
Nov 21 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michele Simionato | last post: by
3 posts views Thread by H Jansen | last post: by
5 posts views Thread by could ildg | last post: by
5 posts views Thread by Ken Schutte | last post: by
1 post views Thread by Daniel Klein | last post: by
reply views Thread by Clarence | last post: by
5 posts views Thread by Sandra-24 | last post: by
4 posts views Thread by Steven D'Aprano | last post: by
reply views Thread by Ethan Furman | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.