第60行,在make-tuple-return-tuple(l)TypeError中:iter()返回了“Vector”类型的非迭代器

2024-06-01 07:11:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我对向量和制作类很陌生。我试图构造自己的向量类,但当我通过代码传递它时,它是:

位置+=航向*移动距离

其中位置和航向都是矢量。航向正常化。我的目标是重复我的代码,直到位置=目的地。 这个班怎么了?

导入数学

class Vector(object):
    #defaults are set at 0.0 for x and y
    def __init__(self, x=0.0, y=0.0):
        self.x = x
        self.y = y

    #allows us to return a string for print
    def __str__(self):
        return "(%s, %s)"%(self.x, self.y)

    # from_points generates a vector between 2 pairs of (x,y) coordinates
    @classmethod
    def from_points(cls, P1, P2):
        return cls(P2[0] - P1[0], P2[1] - P1[1])

    #calculate magnitude(distance of the line from points a to points b
    def get_magnitude(self):
        return math.sqrt(self.x**2+self.y**2)

    #normalizes the vector (divides it by a magnitude and finds the direction)
    def normalize(self):
        magnitude = self.get_magnitude()
        self.x/= magnitude
        self.y/= magnitude

    #adds two vectors and returns the results(a new line from start of line ab to end of line bc)
    def __add__(self, rhs):
        return Vector(self.x +rhs.x, self.y+rhs.y)

    #subtracts two vectors
    def __sub__(self, rhs):
        return Vector(self.x - rhs.x, self.y-rhs.y)

    #negates or returns a vector back in the opposite direction
    def __neg__(self):
        return Vector(-self.x, -self.y)

    #multiply the vector (scales its size) multiplying by negative reverses the direction
    def __mul__(self, scalar):
        return Vector(self.x*scalar, self.y*scalar)

    #divides the vector (scales its size down)
    def __div__(self, scalar):
        return Vector(self.x/scalar, self.y/scalar)

    #iterator
    def __iter__(self):
        return self

    #next
    def next(self):
        self.current += 1
        return self.current - 1

    #turns a list into a tuple
    def make_tuple(l):
        return tuple(l)

Tags: andofthefromselfreturndefline
2条回答

我猜您使用的是Python3.x,因为我有一个类似的错误。 我也是新来上课的,但很高兴能和大家分享我学到的东西:)

在3.x中,在类的定义中使用__next__(),而不是next()。 在您的代码中重命名后,错误没有发生,但我遇到了另一个问题,“'Vector'对象没有'current'属性:”)

我认为你最好理解迭代器(和类?)更多。 最简单的例子是:

class Count:
    def __init__(self, n):
        self.max = n

    def __iter__(self):
        self.count = 0
        return self

    def __next__(self):
        if self.count == self.max:
            raise StopIteration
        self.count += 1
        return self.count - 1

if __name__ == '__main__':
    c = Count(4)
    for i in c:
        print(i, end = ',')

输出为0,1,2,3,。

对于向量类,我想迭代向量的组件。所以:

def __iter__(self):
    self.count = 0
    self.list = [self.x, self.y, self.z]  # for three dimension
    return self

def __next__(self):
    if self.count == len(self.list):
        raise StopIteration
    self.count += 1
    return self.list[self.count - 1]

迭代器输出序列x,y,z

注意,迭代器最重要的特性是在不创建整个列表的情况下逐步给出序列。因此,如果序列很长,那么制作self.list不是很好的主意。 这里有更多细节:python tutorial

传递到make_tuple的第一个参数是您的Vector实例(它与您在任何地方放置的self参数相同)。

你必须把你想变成一个元组的东西传入,这个元组可能是你的x和y坐标:

def make_tuple(self):
    return (self.x, self.y)

相关问题 更多 >