【问题标题】:line 60, in make_tuple return tuple(l) TypeError: iter() returned non-iterator of type 'Vector'第 60 行,在 make_tuple 中返回 tuple(l) TypeError: iter() returned non-iterator of type 'Vector'
【发布时间】:2013-02-15 12:51:12
【问题描述】:

我是 Vectors 和制作课程的新手。我正在尝试构建自己的矢量类,但是当我通过我的代码传递它时:

位置 += 航向*距离移动

其中位置和标题都是向量。标题已标准化。我的目标是重复我的代码,直到位置 = 目的地。 这个类有什么问题?

导入数学

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)

【问题讨论】:

    标签: python class vector iteration pygame


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我猜你使用的是 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 tutorial

      【讨论】:

      • 谢谢,这是为我做的。现在,如果文档不是那么可怕地过时和不完整,我们可能都会避免这种情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2021-12-11
      • 2013-08-25
      相关资源
      最近更新 更多