我正在做一個多項式類,我有一個複製函數的問題。它假定創建Poly對象的副本並返回對新Poly對象的引用。我真的堅持這個副本的想法。感謝您的幫助python中的類定義中的複製函數
class Poly:
def __init__ (self, p):
self.a = p
self.deg= len(p) -1
if len(p) == 1 and p[0] == 0:
self.deg = -1
def evalPoly(self,x0):
''' evaluates the polynomial at value x'''
b=0
for coefficients in reversed(self.a):
b=b*x0+int(coefficients)
return b
def polyPrime(self):
'''replaces the coeffiecients of self with the coefficients
of the derivative polynomial '''
if self.deg == 0:
return np.zeroes(1,float), 0
else:
newdeg=self.deg-1
p=[i*self.a[i] for i in range(1,self.deg+1)]
p=str(p)[1: -1]
p=eval(p)
return p
def copy(self):
return Poly(self.a)
我卡在如何創建多邊形對象的副本,並返回到新保利對象的引用
究竟發生了什麼問題? – sinelaw 2012-04-19 22:22:28