python – 问题子类化内置类型
发布时间:2021-01-17 10:40:52 所属栏目:Python 来源:互联网
导读:# Python 3class Point(tuple): def __init__(self, x, y): super().__init__((x, y))Point(2, 3) 会导致 TypeError: tuple() takes at most 1 argument (2 given) 为什么?我该怎么做呢? 元组是一个不可变类型.在__init__被调用
# Python 3
class Point(tuple):
def __init__(self,x,y):
super().__init__((x,y))
Point(2,3)
会导致
为什么?我该怎么做呢? 解决方法元组是一个不可变类型.在__init__被调用之前,它已经被创建并且不可变.这就是为什么这不起作用.如果您真的想要子类化元组,请使用 >>> 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,3) (编辑:吉安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Python分隔线分割问题
- 在cygwin下,如何配置Mercurial以使用WinMerge进行合并?
- python – PyTables读取随机子集
- python – nvcc致命:没有为theano选项’gpu-architecture’
- python – Django:如何在佛罗里达州的设置中设置EDT时区
- python – 为Matplotlib imshow()手动定义的轴标签
- python – Django中的Slugify字符串
- python – 使用Tensorflow中的多层感知器模型预测文本标签
- python – 基本的openGL,顶点缓冲区和pyglet
- python – 如何在Django 1.9中设置“简单”密码
