TypeError: __init__()取1到3个位置参数,但给出4个

时间:2021-02-25 23:18:25

I have hit a problem with one of the examples in the book Beginning Python Games Development Second Edition.

我遇到了一个问题,其中一个例子在书中开始了Python游戏开发的第二版。

It has me setting up a vector class (full code at the end of the Q) with the following __init__ function:

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

def __init__(self, x=0, y=0):
    self.x = x
    self.y = y

It then asks me to run this code snippet against it:

然后它要求我对它运行这个代码片段:

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)

The result is the following error:

结果是:

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

When I put a print on the *A, it comes up as just 2 numbers, as you would expect. Why is it somehow turning this into 4?

当我在* a上打印时,它会显示为两个数字,正如你所期望的。为什么它会把这个变成4呢?

Full Vector2 code:

满Vector2代码:

import math

class Vector2:

    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __str__(self):
        return "(%s, %s)"%(self.x, self.y)

    def from_points(P1, P2):
        print("foo")
        return Vector2( P2[0] - P1[0], P2[1] - P1[1])

    def get_magnitude(self):
        return math.sqrt( self.x**2 + self.y**2 )

    def normalise(self):
        magnitude = self.get_magnitude()
        self.x /= magnitude
        self.y /= magnitude

    # rhs stands for right hand side
    def __add__(self, rhs):
        return Vector2(self.x + rhs.x, self.y + rhs.y)

    def __sub__(self, rhs):
        return Vector2(self.x - rhs.x, self.y - rhs.y)

    def __neg__(self):
        return Vector2(-self.x, -self.y)

    def __mul__(self, scalar):
        return Vector2(self.x * scalar, self.y * scalar)

    def __truediv__(self, scalar):
        return Vector2(self.x / scalar, self.y / scalar)

2 个解决方案

#1


3  

A contains three elements, (10.0, 20, 0), because you used a comma, not a . decimal point when defining it:

A包含三个元素(10.0、20、0),因为您使用的是逗号,而不是A。定义时小数点:

A = (10.0, 20,0)
#            ^ that's a comma

Together with the self argument that means you passed in 4 arguments to the __init__ method.

连同self参数,这意味着您将4个参数传递给__init__方法。

#2


0  

A = (10.0, 20,0)

you should use

你应该使用

A = (10.0, 20.0)

so when you do instanciate

所以当你这样做的时候

Vector(*A)

you dont pass 4 arguments (self, 10.0, 20, 0) but 3 arguments (self, 10.0, 20.0)

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

#1


3  

A contains three elements, (10.0, 20, 0), because you used a comma, not a . decimal point when defining it:

A包含三个元素(10.0、20、0),因为您使用的是逗号,而不是A。定义时小数点:

A = (10.0, 20,0)
#            ^ that's a comma

Together with the self argument that means you passed in 4 arguments to the __init__ method.

连同self参数,这意味着您将4个参数传递给__init__方法。

#2


0  

A = (10.0, 20,0)

you should use

你应该使用

A = (10.0, 20.0)

so when you do instanciate

所以当你这样做的时候

Vector(*A)

you dont pass 4 arguments (self, 10.0, 20, 0) but 3 arguments (self, 10.0, 20.0)

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