Append methods in a list - Python

Asked

Viewed 1,871 times

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?

  • 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

  • What exactly did you intend to do then? In your loop, each call method assigns the var of the object self, overwriting the previous value. So when you do self.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 calling methodList[1] somehow the side effect was to alter self.var)? It seems to me that you tried to do a partial application of functions, but I’m not sure...

  • yes, wanted to try to apply the methodList[1] and call the setVar(1), but was not succeeding, the gypsy helped, thanks to all

  • 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.

2 answers

3

If what you want is fix some arguments of a method (as its list calls methodList, I imagine you want to keep in it methods, not values) you can use functools.partial. In it you set a fixed number of arguments, and take back a "partial function":

def setVar(self, number):
    self.var = number

methodList = list()

for y in range(4):
    methodList.append(functools.partial(setVar, self, y))

self.var = 0
methodList[1]() # self.var agora deve ser 1

2


Its function does not print anything. So it does not return anything in print.

I made some corrections:

def setVar(number):
    var = number
    return var

methodList = list()

for y in range(4):
    methodList.append(setVar(y))

print(methodList[1])

You don’t have to use self for your example why you did not specify a class.

See here working.

  • I have a bigger problem now, but it’s the same thing(almost): self.canvasButtonCommands.append(setMusics(self, genreDirectory[a])) ---------- self.canvasButtonCommands[0] does nothing, but his code is written to do...

  • Another problem deserves another question, right?

  • I can not create a question before 90 minutes '-', from a help Plz, I think it has to do with the [a]

  • Are you sure you don’t do anything? Have you tried putting a print within the method to check?

  • I have already discovered the error, every time I do this: self.canvasButtonCommands.append(setMusics(self, genreDirectory[a])) ---- it calls the function, but it was only to give append

  • Append without parameters. Use the parameters when calling. If necessary, use an auxiliary structure with the method and separate parameters.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.