problem subclassing builtin type

风流意气都作罢 提交于 2019-12-01 17:59:19

tuple is an immutable type. It's already created and immutable before __init__ is even called. That is why this doesn't work.

If you really want to subclass a tuple, use __new__.

>>> class MyTuple(tuple):
...     def __new__(typ, itr):
...             seq = [int(x) for x in itr]
...             return tuple.__new__(typ, seq)
... 
>>> t = MyTuple((1, 2, 3))
>>> t
(1, 2, 3)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!