python3中运行报错

来源:7-1 如何派生内置不可变类型并修改实例化行为

weibo_字无忌_0

2017-01-08

class IntTuple(tuple):
    def __new__(cls, iterable):
        g = (x for x in iterable if isinstance(x,int) and x > 0)
        return super(IntTuple,cls).__new__(cls,g)
    def __init__(self,iterable):
        super(IntTuple,self).__init__(iterable)

t = IntTuple([1,-1,"abc",6,["x","y"],3])
print(t)

Traceback (most recent call last):

  File "C:/Users/wang/PycharmProjects/untitled5/python慕课网教程总结/__new__方法的使用.py", line 19, in <module>

    t = IntTuple([1,-1,"abc",6,["x","y"],3])

  File "C:/Users/wang/PycharmProjects/untitled5/python慕课网教程总结/__new__方法的使用.py", line 17, in __init__

    super(IntTuple,self).__init__(iterable)

TypeError: object.__init__() takes no parameters


写回答

1回答

程序员硕

2017-01-08

不需要实现__init__

2
0

Python高效编程技巧实战

精选50个Python案例,源自实战,全面提升Python编程能力

2582 学习 · 360 问题

查看课程