1
I tried to add 4 methods to a list(methodList):
def setVar(self, number):
self.var = number
methodList = list()
for y in range(4):
methodList.append(setVar(self, y))
and the function does not work:
>>> self.var = 0
>>> methodList[1]
>>> print(self.var)
0
if it worked it would be like this:
>>> self.var = 0
>>> methodList[1]
>>> print(self.var)
1
then I printed the function to see:
>>> print(methodList[1])
None
Where I went wrong?
This first code, you ran it only once, and before you tried this next code on the console, right?
– mgibsonbr
yes, actually it was all in the code, I’m only using the arrows to represent my problem, but there is no order error in the lines
– Radagast
What exactly did you intend to do then? In your loop, each call method assigns the
var
of the objectself
, overwriting the previous value. So when you doself.var = 0
you overwrite it once more. Did you want each value to go to a list position? (if yes, see the response of the Gypsy) Or was it something else I intended (for example, when callingmethodList[1]
somehow the side effect was to alterself.var
)? It seems to me that you tried to do a partial application of functions, but I’m not sure...– mgibsonbr
yes, wanted to try to apply the methodList[1] and call the setVar(1), but was not succeeding, the gypsy helped, thanks to all
– Radagast
I don’t understand anything (because if your list calls
methodList
, why does it hold values? ), but I left an answer anyway taking the second case.– mgibsonbr