Skip to main content
 首页 » 编程设计

python之类型错误 : list() takes at most 1 argument (3 given) for list class inheritance

2025年05月04日14sky-heaven

我在 stackoverflow 中搜索了其他帖子,甚至将它们复制到我的机器上作为答案进行尝试。然而,它总是失败抛出 "TypeError"

# this is as one of other post in StackOverflow.  
class ListClass(list): 
    def __init__(self, *args): 
        super().__init__(self, *args) 
        self.append('a') 
        self.name = 'test' 

我也试过通过空课。但是,如果我继承并且我想我错过了一些东西而不是添加更多或错误的东西,那也会失败?

1) 这是什么 "TypeError" 以及为什么? 2) 如何解决?

关于 TypeError 的进一步快照:

>>> class ListClass(list): 
...     pass 
... 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
TypeError: list() takes at most 1 argument (3 given)   

请您参考如下方法:

当您创建 ListClass 对象时,您将它赋予许多参数,这就是您正在做的:

s = list(2,3,3) 

这是你应该做的:

s = list([2,3,3]) 

在你的解释器上试试这个片段。