Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:只接受2个参数(给定1个) >>x=DoublyLinkedList >>>x.添加_头(1) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 x、 添加_头(1) TypeError:add_head()正好接受2个参数(给定1个)_Python - Fatal编程技术网

Python TypeError:只接受2个参数(给定1个) >>x=DoublyLinkedList >>>x.添加_头(1) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 x、 添加_头(1) TypeError:add_head()正好接受2个参数(给定1个)

Python TypeError:只接受2个参数(给定1个) >>x=DoublyLinkedList >>>x.添加_头(1) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 x、 添加_头(1) TypeError:add_head()正好接受2个参数(给定1个),python,Python,猜测x不算作参数,因为它没有正确实例化 >>> x=DoublyLinkedList >>> x.add_head(1) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> x.add_head(1) TypeError: add_head() takes exactly 2 arguments (1 given) 不是 不

猜测x不算作参数,因为它没有正确实例化

>>> x=DoublyLinkedList
>>> x.add_head(1)
Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    x.add_head(1)
TypeError: add_head() takes exactly 2 arguments (1 given)
不是

不包括parens将
双链接列表
模块对象指定给x,它不会创建新对象。

当您编写

x = DoublyLinkedList
将类
DoublyLinkedList
指定给
x
,而不是它的实例
add_head
作为实例方法,不能直接在类上调用。相反,您需要使用

x = DoublyLinkedList

这样,python将能够用
x
替换
self
,并且您的调用将有两个参数。

在分配给x时,您需要创建对象的实例

x = DoublyLinkedList()
x.add_head(1)

前一种语法(DoubleLinkedList)是允许的,因为类作为对象是有效的。

对您正在做的事情进行一点解释,而不仅仅是一些代码,这将是一种更好的提问方式。目前,读者不得不猜测这个问题。
x = DoublyLinkedList
x = DoublyLinkedList()
x.add_head(1)
 x = DoublyLinkedList()