2013-03-01 4 views
13

Jestem nowy wektory i tworzenie klas. Staram się budować moje własne klasy wektorowych ale kiedy przekazać go za pośrednictwem mojego kodu, który jest:linia 60, w make_tuple return tuple (l) TypeError: iter() zwrócił nie-iterator typu 'Vector'

pozycja + = nagłówek * distance_moved

gdzie pozycję i są oba wektory. nagłówek jest znormalizowany. moim celem jest powtórzenie mojego kodu do pozycji = miejsce docelowe. Co jest nie tak z tą klasą?

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) 

Odpowiedz

0

Pierwszy argument, który jest były przekazywane do make_tuple jest instancję Vector (to samo self argument, że można umieścić wszędzie).

musiał przejść w co chcesz przekształcić krotki, która jest prawdopodobnie współrzędne X i Y:

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

Chyba używasz Pythona 3.x, ponieważ mam Mam podobny błąd. Jestem również na tworzeniu nowej klasy, ale byłoby miło, aby dzielić się dowiedziałem :)

W 3.x, należy __next__() zamiast next() w definicji klas. Błąd nie pojawił się po zmianie jego nazwy w kodzie, ale mam inny problem, "Obiekt" Wektor "nie ma atrybutu" bieżący "" :)

Myślę, że może lepiej dla Ciebie zrozumieć iteratory (i klasa?) więcej. Naprostrzym przykładem jest:

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 = ',') 

a wyjścia 0,1,2,3 ,.

Za pomocą klasy wektorowej chcę iterować komponenty wektora. A więc:

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] 

a iterator wyprowadza sekwencję x, y, z.

Należy zauważyć, że najważniejszą cechą iteratorów jest stopniowe przekazywanie sekwencji bez tworzenia całej listy. Więc nie jest dobrym pomysłem, aby zrobić self.list, jeśli sekwencja będzie bardzo długa. Więcej szczegółów tutaj: python tutorial

+0

Dzięki, zrobiłem to dla mnie. Gdyby tylko doktorzy nie byli tak potwornie nieaktualni i niekompletni, wszyscy moglibyśmy tego uniknąć. – Basic

Powiązane problemy