I'm trying to create a subclass of tuple which behaves in all aspects like a normal tuple with the exception that I initialize it with a string which is automatically split first by the constructor (I also want its __str__() to join this again, but that's not the issue here).
I thought this to be straight-forward and tried it like this:
class C(tuple):
def __init__(self, text):
super(C, self).__init__(text.split(':'))
def __str__(self):
return '[%s]' % ':'.join(self)
c = C('one:two:three') # I expect this to be like ('one', 'two', 'three')
So I try to pass a text (a str), split that and call my superclass's constructor with the result. I expected to get a result like for tuple([ 'one', 'two', 'three' ]), i. e. a tuple of words: ('one', 'two', 'three').
But instead I get a tuple of characters, i. e. for the input 'one:two:three' I get a ('o', 'n', 'e', ':', 't', 'w', 'o', ':', 't', 'h', 'r', 'e', 'e') which is exactly the result I get when I call tuple('one:two:three').
I debugged the situation and found out that my code gets properly executed (my __init__() gets called and calls the other __init__() with the correct values). I also tried to replace the super construct by a concrete tuple.__init__(self, text.split(':')), but that didn't change anything. I also tried passing a tuple instead of the list created by split(), also no change. Actually, calling super's __init__() does not seem to have any effect. The interpreter still initializes the tuple with the string I pass originally.
Am I missing something? Why is this not working as expected? How can I create a class C which is a subclass of tuple and which I can initialize by calling C('one:two:three') to get an instance of C which is a tuple like ('one', 'two', 'three')?