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

Python TypeError:\uuuu init\uuuuu()接受1到3个位置参数,但给出了4个

Python TypeError:\uuuu init\uuuuu()接受1到3个位置参数,但给出了4个,python,Python,我在书中的一个例子中遇到了一个问题 它让我使用以下\uuuuu init\uuuu函数设置一个向量类(Q末尾的完整代码): def __init__(self, x=0, y=0): self.x = x self.y = y 然后,它要求我对其运行以下代码段: from Vector2 import * A = (10.0, 20,0) B = (30.0, 35.0) AB = Vector2.from_points(A, B) step = AB * .1 positi

我在书中的一个例子中遇到了一个问题

它让我使用以下
\uuuuu init\uuuu
函数设置一个向量类(Q末尾的完整代码):

def __init__(self, x=0, y=0):
    self.x = x
    self.y = y
然后,它要求我对其运行以下代码段:

from Vector2 import *

A = (10.0, 20,0)
B = (30.0, 35.0)
AB = Vector2.from_points(A, B)
step = AB * .1
position = Vector2(A, B)
step = AB * .1
print(*A)
position = Vector2(*A)
for n in range(10):
    position += step
    print(position)
结果是以下错误:

Traceback (most recent call last):
  File "C:/Users/Charles Jr/Dropbox/Python/5-14 calculating positions.py", line 10, in <module>
    position = Vector2(*A)
TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given
A
包含三个元素,
(10.0,20,0)
,因为您在定义它时使用了逗号,而不是
小数点:

A = (10.0, 20,0)
#            ^ that's a comma
self
参数一起,这意味着您向
\uuuu init\uuu
方法传递了4个参数

A = (10.0, 20,0)
你应该使用

A = (10.0, 20.0)
所以当你进行实例化时

Vector(*A)

您不会传递4个参数(self,10.0,20,0),而是传递3个参数(self,10.0,20.0)

谢谢。这真是一个很难发现的问题!
Vector(*A)