0

[Solved] Append method as a variable?

I want to assign an append method to a variable (line 63) and then use it on line 161 instead of pw.append(random.choice(low)). Doing it this way does not work, however. The variable will not execute. Furthermore, it executes the method right on line 63 forcing me to reset the pw list (line 77). What would be the correct approach? https://code.sololearn.com/c27GKL0AWxyE/?ref=app

18th Oct 2020, 6:53 AM
Adam Stork
Adam Stork - avatar
5 Answers
+ 3
A function can be assigned to a variable. For example: gen_low = lambda: pw.append(random.choice(low)) And then invoke it like this: gen_low() But generally it is better for readability and nicer for structuring the code, to create a new function def gen_low(): pw.append(random.choice(low)) This has exactly the same effect as assigning the lambda to a variable...
18th Oct 2020, 7:11 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Tibor Santa Very helpful! Thank you, Tibor!
18th Oct 2020, 7:13 AM
Adam Stork
Adam Stork - avatar
+ 1
codemonkey Haha, don't do that, bud!
18th Oct 2020, 7:15 AM
Adam Stork
Adam Stork - avatar
0
codemonkey I see! Thank you for the quick reply! So basically, if I understand correctly, methods and functions cannot be assigned to variables but rather have to be defined as functions?
18th Oct 2020, 7:04 AM
Adam Stork
Adam Stork - avatar
0
codemonkey That's okay, I get the gist. :) That is very helpful. Thank you!
18th Oct 2020, 7:08 AM
Adam Stork
Adam Stork - avatar