__str__(self, index)
Verfasst: Dienstag 6. November 2007, 15:17
ich habe mir ne kleine Vektorkalsse geschriben:
so in diesem script habe ich mir nen vector definiert und ihn mit print ausgegeben:
funktioniert auch jetzt das problem ich habe mir in nem zweiten modul ne liste mit vectoren geschriben und wollte die per print ausgeben und jetzt bekomme ich als ausgabe vector instance at...
Code vom zweiten modul:
hat jemand ne ahnung wieso nicht und kann mir sagen was ich ändern muss
Code: Alles auswählen
class Vector3D:
def __init__(self):
self.v=[0,0,0]
def fill(self,x=0,y=0,z=0):
self.v[0]=x
self.v[1]=y
self.v[2]=z
return self
def __add__(self,other):
self.v[0]+=other.v[0]
self.v[1]+=other.v[1]
self.v[2]+=other.v[2]
return self
def __sub__(self, other):
self.v[0]-=other.v[0]
self.v[1]-=other.v[1]
self.v[2]-=other.v[2]
return self
def __mul__(self,other):
try:
#other is a vector
self.v[0]=self.v[1]*other.v[2]-self.v[2]*other.v[1]
self.v[1]=self.v[2]*other.v[0]-self.v[0]*other.v[2]
self.v[2]=self.v[0]*other.v[1]-self.v[1]*other.v[0]
except:
#other is a scalar
self.v[0]*=other
self.v[1]*=other
self.v[2]*=other
return self
def length(self):
return(round(sqrt(self.v[0]**2+self.v[1]**2+self.v[2]**2),6))
def normalize(self):
length=self.length()
self.v[0]=self.v[0]/length
self.v[1]=self.v[1]/length
self.v[2]=self.v[2]/length
return self
def show(self):
print "[",self.v[0],"]"
print "[",self.v[1],"]"
print "[",self.v[2],"]"
def __str__(self):
string="["+str(self.v[0])+"]\n["+str(self.v[1])+"]\n["+str(self.v[2])+"]"
return string
def __getitem__(self, index):
try:
return self.v[index]
except:
print "Error index out of range it's a 3D Vector"
def scalarproduct(self,other):
print "not yet implementet"
Code: Alles auswählen
...
vec1.fill(2,0,0)
print "Vector mit print"
print vec1
Code vom zweiten modul:
Code: Alles auswählen
...
for i in range(nat):
l=coords[i]
vec=Vector3D()
vec.fill(l[0],l[1],l[2])
molecule.append(vec)
...
for i in range(nat):
print sym[i]
molecule[i].show()#funktioniert
print molecule[i] #funktioniert nicht
...