2013-03-01 8 views
13

私はベクトルとクラスを初めて使用しました。私は私のコードを通してそれを通過したとき、私はある私自身のベクトルのクラスを構築しようとしているが、午前:+ =見出しライン60、make_tuple return tuple(l)TypeError:iter()は 'Vector'タイプの反復子を返しませんでした

位置は*位置と見出しが両方のベクトルで

をdistance_moved。見出しは正規化されます。私の目標は、position = destinationまで自分のコードを繰り返すことです。 このクラスで何が問題になっていますか?

輸入数学

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) 

答えて

0

make_tupleに渡されています最初の引数は、(それはあなたがどこにでも置く同じself引数です)あなたのVectorインスタンスです。

あなたはおそらくあなたのx、y座標であるタプル、に変身したいものに合格する必要があり:

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

私はあなたがPythonの3.xを使用していると思い、私がしましたので、同様のエラーが発生しました。 クラスの定義に代わりnext()__next__()を使用し、私はまた、クラスを作る上で、新たなんだけど、私が3.xのでは:​​)

を学んだことを共有するためにいいだろう。 コード内で名前を変更してもエラーは発生しませんでしたが、別の問題があります。「 'Vector'オブジェクトには「現在の」属性がありません」:

イテレータを理解する方が良いかもしれない(そしてクラス?)more。

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

+0

ありがとう、これは私のためでした。ドキュメントがあまりにも時代遅れでなく、不完全であったとしても、私たちはこれをすべて避けたかもしれません。 – Basic

関連する問題