Python shuffle

I am trying to shuffle an array of functions in python. My code is as follows:

import random def func1(): ... def func2(): ... def func3(): ... x=[func1,func2,func3] y=random.shuffle(x) 

And I think this can work, the fact is that I don’t know how to call functions after I shuffled the array!

if I write "y" after the last line, it does nothing!

thanks

+4
source share
1 answer

First, random.shuffle() shuffles the list into place. It does not return a shuffled list, so y = None . That's why it does nothing when you type y .

To call each function, you can go through x and call each function as follows:

 for function in x: function() # The parentheses call the function 

Finally, your functions actually throw a SyntaxError. If you want them to do nothing, add pass at the end of them. pass does nothing and is placed where python is expecting something.


So generally:

 def func1(): pass def func2(): pass def func3(): pass x = [func1, func2, func3] random.shuffle(x) for function in x: function() 
+14
source

Source: https://habr.com/ru/post/1502338/


All Articles