第60行,make_tuple返回tuple(l) TypeError: iter()返回的非迭代器类型为'Vector

14 投票
2 回答
12938 浏览
提问于 2025-04-17 17:32

我刚接触向量和类的概念。我正在尝试自己构建一个向量类,但当我在代码中使用它时,代码是:

position += heading*distance_moved

其中 position 和 heading 都是向量,heading 是标准化的。我的目标是重复这段代码,直到 position 等于 destination。这个类有什么问题呢?

import math

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)

2 个回答

0

传递给 make_tuple 的第一个参数是你的 Vector 实例(就是你到处使用的那个 self 参数)。

你需要传入你想要转换成元组的内容,可能就是你的 x 和 y 坐标:

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

我猜你在用Python 3.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教程

撰写回答