Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 super:TypeError:\uuuu init\uuuuu()接受2个位置参数,但给出了3个_Python_Python 3.x - Fatal编程技术网

python super:TypeError:\uuuu init\uuuuu()接受2个位置参数,但给出了3个

python super:TypeError:\uuuu init\uuuuu()接受2个位置参数,但给出了3个,python,python-3.x,Python,Python 3.x,每个人我都在练习Python,我发现了一些奇怪的东西,这是我的代码 逻辑学 但是,下面显示了错误,我使用的是python 3.6.4 在我标记出代码“超级”后,它可以正常工作 super(AndGate,self)。init(self,n) 上面这些代码是我从python研究中复制的,请参见清单11中的站点,它显示了这些代码的工作原理,但当我复制到我的计算机上时,代码不工作??我必须标出“超级”部分??为什么?多谢各位 您不需要在此处传递self: super(AndGate,self).__

每个人我都在练习Python,我发现了一些奇怪的东西,这是我的代码

逻辑学

但是,下面显示了错误,我使用的是python 3.6.4

在我标记出代码“超级”后,它可以正常工作

super(AndGate,self)。init(self,n)

上面这些代码是我从python研究中复制的,请参见清单11中的站点,它显示了这些代码的工作原理,但当我复制到我的计算机上时,代码不工作??我必须标出“超级”部分??为什么?多谢各位


您不需要在此处传递
self

super(AndGate,self).__init__(self,n)
应该是

super(AndGate,self).__init__(n)

使用
super
时,会自动传递
self

此外,在Python3.3及更高版本中,
super
甚至不需要接收参数就可以知道它是从哪个类调用的。你可以简单地做到这一点

super().__init__(n)

这大大提高了可维护性,因此这将是首选方法。

如果您使用的是Python 3.3及更高版本,则应替换

LogicGate.__init__(self,n)

当您想要调用超级类构造函数时,最好使用此格式。

相关:
super().__init__(n)
LogicGate.__init__(self,n)
super().__init__(n)