Friday, 15 July 2011

python - Create an object of the same class inside the class -



python - Create an object of the same class inside the class -

i trying create polynomial class derivative new polynomial. doing wrong somewhere because i'm modifying polynomial...

here code:

class polynomial(object): def __init__(self, coeffs): self._coeffs = coeffs self._df = none def __repr__(self): #blahblah def f(self, x): #blahblah def __call__(self,x): homecoming self.f(x) def df(self): if self._df == none: dcoeffs = self._coeffs[:-1] in range(len(dcoeffs)): dcoeffs[i] *= len(dcoeffs)-i self._df = self.__init__(dcoeffs) homecoming self._df

i pretty sure doing wrong when phone call self.__init__(dcoeffs) not know how create polynomial within same class. there proper way or python (2.6.6) not allow this?

thank much help.

there's no special syntax create class instance within own class definition. utilize name, same anywhere else.

class polynomial(object): def __init__(self, coeffs): self._coeffs = coeffs def __repr__(self): #blahblah def f(self, x): #blahblah def __call__(self,x): homecoming self.f(x) def df(self): dcoeffs = self._coeffs[:-1] in range(len(dcoeffs)): dcoeffs[i] *= len(dcoeffs)-i homecoming polynomial(dcoeffs)

python

No comments:

Post a Comment