I am turning some piece of Python code into OO-code. I define a class and a two arguments constructor.
class myclass:
MAX=5000
FTOL = 10**(-10)
TINY = 10**(-10)
def __init__(self, initPt_,fun_):
self.initPt = initPt_
self.fun = fun_
With this code, I expect initPt and fun to be class members. I execute the code and write this instanciation in shell:
x=myclass([1,1],Optim.fun1)
where fun1 is a function defined in module Optim, that I accurately imported.
I added to myclass a function:
def build(self):
self.initPt=self.initPt.append(self.fun(self.initPt))
s=[self.initPt]
for k in range(len(self.initPt)):
temp=[x for x in self.initPt]
temp[k]=self.initPt[k]+1
s.append(temp)
return s
I call method build on the instance x I just created. This error appears:
for k in range(len(self.initPt)):
TypeError: object of type 'NoneType' has no len()
and indeed, if I add this test line in code:
print str(type(self.iniPt))
shell indicates that type is NoneType, althought I instanciated x with self.initPt = [1,1].
Moreover, if I write x.initPt in shell, result is accurate: [1,1,0.2] (after call of method build, third value is added to initial list [1,1]).
I don’t understand why it is not resolving type dynamically. It is not object programming then. What should I do?
Thanks and regards.
This is at least part of your problem:
Using the
appendmethod of a list modifies the list in place and returnsNone. So this replaces the list assigned toself.initPtwithNone. Just do this: