Ich habe ein ganz seltsames Problem mit meinem Skript!
Die Ausgabe am Ende lautet:
(x,y,z) : (5.000,6.000,7.000)
(x,y,z) : (5.000,6.000,7.000)
(x,y,z) : (5.000,6.000,7.000)
Eigentlich bin ich das Meinung, daß dies die Ausgabe sein müsste:
(x,y,z) : (0.000,0.000,0.000)
(x,y,z) : (1.000,2.000,3.000)
(x,y,z) : (5.000,6.000,7.000)
...Vielleicht kann mir ja jmd. bei meinem grundlegendem Verständnis für Python helfen... ich kann's mir nicht erklären!
Code: Alles auswählen
# =======================================================================
# Class: coords
# generalyzed coords
# =======================================================================
class coords:
""" Generalyzed space coordinates """
# ===== member variables
coList = [0.0,0.0,0.0] # internal data structure, external: c0, c1, c2
# ===== construktor
def __init__(self, c0=0.0 ,c1=0.0 ,c2=0.0):
"""Constructor"""
self.coList[0] = c0
self.coList[1] = c1
self.coList[2] = c2
# ===== representation
def __repr__(self):
"""
Representation of coords
- rounded on 3 digits
"""
return "(%.3f,%.3f,%.3f)" % (self.coList[0],self.coList[1],self.coList[2] )
# ===== get Index
# - help method for getattr/setattr
def getIndex(self,attr):
"""
getIndex
help method fot getattr / setattr
"""
c = attr[1]
# check for errors
if len(attr)!=2:
pass # Error: length not 2
elif c in '012':
return '012'.index(c)
else:
pass # Error: not 0,1,2
# still here <-> error occured
print "found: ",attr
print "valid attributes are c0, c1, c2"
raise AttributeError, "unknown attribute"
# ===== get Attributes
def __getattr__(self, attr):
"""
get Attribute
- attr: c0,c1,c2
- returns an attribute
"""
if attr=="__str__":
return self.__str__()
else:
ind = self.getIndex(attr)
return self.coList[ind]
# ===== set Attributes
def __setattr__(self, attr,value):
"""
set Attribute
- attr: c0,c1,c2
- sets an attribute
"""
ind = self.getIndex(attr)
self.coList[ind] = value
# ======================================================================
# Class: decart
# cartesian coords
# =======================================================================
class decartCoords(coords):
"""Decart xyz coordinates"""
# ===== constructor
def __init__(self, x=0.0, y=0.0, z=0.0):
coords.__init__(self, x,y,z)
# ===== representation
def __repr__(self):
"""
Representation of cartesian coords
"""
return "(x,y,z) : %s" % coords.__repr__(self)
# ===== string
def __str__(self):
return self.__repr__()
# =======================================================================
# Execution
# =======================================================================
if __name__ == "__main__":
print "Run script"
t0 = decartCoords()
t1 = decartCoords(1,2,3)
t2 = decartCoords(5,6,7)
print t0
print t1
print t2
Vielen Dank!
Gruß, Tobias