__new__ woes with list

macaronikazoo macaronikazoo at gmail.com
Fri Nov 21 16:32:45 EST 2008


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 )



More information about the Python-list mailing list