para que me entiendan mejor aqui dejo el codigo:
Código python:
Ver original
import scipy as sp import scipy.linalg as linalg class solution: def __init__(self): self.feed_flow = 70.0 self.feed = sp.array([0.15,0.25,0.4,0.2]) self.comp_names = ("xylene", "styrene", "toluene", "benzene") self.var=("D_1", "B_1", "D_2", "B_2") self.xylene = sp.array([0.07,0.18,0.15,0.24]) self.styrene = sp.array([0.04,0.24,0.1,0.65]) self.toluene = sp.array([0.54,0.42,0.54,0.1]) self.benzene = sp.array([0.35,0.16,0.21,0.01]) T0_comp_frac = self.feed_flow * self.feed # component fraction on first tower (T0) self.comp_eq = sp.array([self.xylene, self.styrene, self.toluene, self.benzene]) #components linear equations self.solve= sp.array(linalg.solve(self.comp_eq, T0_comp_frac)) self.D = self.solve[0] + self.solve[1] self.B = self.solve[2] + self.solve[3] def comp(self): print "D= %s" % self.D print "B= %s" % self.B for i in range(len(self.var)): print self.var[i], "=", self.solve[i] flowname= ("D","B") x = 0 while x<= 3: y=0 z=0 while y<=2: temp = (self.comp_eq[x,y]*self.solve[y] + self.comp_eq[x,y+1]*self.solve[y+1])/(self.solve[y]+self.solve[y+1]) y+=2 print self.comp_names[x]+ " in "+ flowname[z]+" = ", temp z+=1 x+=1 class solution2(solution): def __init__(self, percent): self.feed_flow = 70.0 - 70.0 * percent if __name__=="__main__": solution=solution() solution.comp() print "*" * 80 percent = 0.01 solution2=solution2(0.01) solution2.comp()
el problema lo tengo es exactamente en esta parte del codigo:
Código Python:
Ver original
class solution2(solution): def __init__(self, percent): self.feed_flow = 70.0 - 70.0 * percent
lo que deseo hacer es especificar una clase "hijo" que herede todos los atributos de la clase "padre" solution menos el parametro feed_flow
el problema es que me manda este error:
Código:
por lo que entiendo me esta indicando que al ejecutar la funcion comp() dentro de la clase solution2 no encuentra el parametro self.DTraceback (most recent call last): File "C:\Python25\destilacion3.py", line 50, in <module> solution2.comp() File "C:\Python25\destilacion3.py", line 22, in comp print "D= %s" % self.D AttributeError: solution2 instance has no attribute 'D'
lo que no entiendo es porque ocurre este problema si supuestamente la clase solution2 hereda los parametros de la clase solution
hasta los momentos he logrado que el codigo corra adecuadamente reescribiendo TODOS los parametros de la clase base en solution2 de la siguiente manera
Código Python:
Ver original
import scipy as sp import scipy.linalg as linalg class solution: def __init__(self): self.feed_flow = 70.0 self.feed = sp.array([0.15,0.25,0.4,0.2]) self.comp_names = ("xylene", "styrene", "toluene", "benzene") self.var=("D_1", "B_1", "D_2", "B_2") self.xylene = sp.array([0.07,0.18,0.15,0.24]) self.styrene = sp.array([0.04,0.24,0.1,0.65]) self.toluene = sp.array([0.54,0.42,0.54,0.1]) self.benzene = sp.array([0.35,0.16,0.21,0.01]) T0_comp_frac = self.feed_flow * self.feed # component fraction on first tower (T0) self.comp_eq = sp.array([self.xylene, self.styrene, self.toluene, self.benzene]) #components linear equations self.solve= sp.array(linalg.solve(self.comp_eq, T0_comp_frac)) self.D = self.solve[0] + self.solve[1] self.B = self.solve[2] + self.solve[3] def comp(self): print "D= %s" % self.D print "B= %s" % self.B for i in range(len(self.var)): print self.var[i], "=", self.solve[i] flowname= ("D","B") x = 0 while x<= 3: y=0 z=0 while y<=2: temp = (self.comp_eq[x,y]*self.solve[y] + self.comp_eq[x,y+1]*self.solve[y+1])/(self.solve[y]+self.solve[y+1]) y+=2 print self.comp_names[x]+ " in "+ flowname[z]+" = ", temp z+=1 x+=1 class solution2(solution): def __init__(self, percent): self.feed_flow = 70.0 - 70.0 * percent self.feed = sp.array([0.15,0.25,0.4,0.2]) self.comp_names = ("xylene", "styrene", "toluene", "benzene") self.var=("D_1", "B_1", "D_2", "B_2") self.xylene = sp.array([0.07,0.18,0.15,0.24]) self.styrene = sp.array([0.04,0.24,0.1,0.65]) self.toluene = sp.array([0.54,0.42,0.54,0.1]) self.benzene = sp.array([0.35,0.16,0.21,0.01]) T0_comp_frac = self.feed_flow * self.feed # component fraction on first tower (T0) self.comp_eq = sp.array([self.xylene, self.styrene, self.toluene, self.benzene]) #components linear equations self.solve= sp.array(linalg.solve(self.comp_eq, T0_comp_frac)) self.D = self.solve[0] + self.solve[1] self.B = self.solve[2] + self.solve[3] if __name__=="__main__": solution=solution() solution.comp() print "*" * 80 percent = 0.01 solution2=solution2(0.01) solution2.comp()
que estoy haciendo mal porque la segunda clase no hereda los parametros de la clase superior???