Subclassing tuple where some of these subclass instances may need to be one-string instances throws up something interesting.
class Sequence( tuple ):
def __init__( self, *args ):
# initialisation...
self.instances = []
def __new__( cls, *args ):
for arg in args:
assert isinstance( arg, unicode ), '# arg %s not unicode' % ( arg, )
if len( args ) == 1:
seq = super( Sequence, cls ).__new__( cls, ( args[ 0 ], ) )
else:
seq = super( Sequence, cls ).__new__( cls, args )
print( '# END new Sequence len %d' % ( len( seq ), ))
return seq
NB as I learnt from this thread, you have to put the comma after args[ 0 ]
.
The print line shows that a single string does not get split up.
NB the comma in the constructor of the subclass now becomes optional :
Sequence( u'silly' )
or
Sequence( u'silly', )