使用 str 为 init 子类化元组

Subclassing Tuple with str for init

我正在尝试创建一个 tuple 的子class,它在所有方面的行为都像普通的 tuple,除了我用一个自动初始化的字符串初始化它首先由构造函数拆分(我也希望其 __str__() 再次加入,但这不是这里的问题)。

我认为这是直截了当的,并像这样尝试过:

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')

所以我尝试传递一个 text(一个 str),拆分它并用结果调用我的 superclass 的构造函数。我希望得到像 tuple([ 'one', 'two', 'three' ]) 这样的结果,我。 e.单词元组:('one', 'two', 'three').

但是我得到了一个字符元组,i。 e.对于输入 'one:two:three' 我得到一个 ('o', 'n', 'e', ':', 't', 'w', 'o', ':', 't', 'h', 'r', 'e', 'e') 这正是我调用 tuple('one:two:three').

时得到的结果

我调试了情况并发现我的代码得到了正确执行(我的 __init__() 被调用并使用正确的值调用另一个 __init__())。我还尝试用具体的 tuple.__init__(self, text.split(':')) 替换 super 构造,但这并没有改变任何东西。我还尝试传递 tuple 而不是 split() 创建的 list,也没有任何变化。其实调用super的__init__()好像没有什么效果。解释器仍然用我最初传递的字符串初始化元组。

我错过了什么吗?为什么这不能按预期工作?我如何创建一个 class C ,它是 tuple 的子 class 并且我可以通过调用 C('one:two:three') 来初始化它以获得 [=29 的实例=] 这是一个像 ('one', 'two', 'three')?

这样的元组

因为元组是不可变的,所以使用 __new__ 而不是 __init__:

class C(tuple):
    def __new__(cls, text):
        return super(C, cls).__new__(cls, text.split(':'))

    def __str__(self):
        return '[%s]' % ':'.join(self)

c = C('one:two:three')
print(c)
print(list(c))