直接上代码!:
import sys
import random
def wait_input(a, b):
print('调用了wait_input')
with open('random_funcs.txt', 'a', encoding='utf8') as f:
f.write('调用了wait_input' + '\n')
def batch_input(a, b):
print('调用了batch_input')
with open('random_funcs.txt', 'a', encoding='utf8') as f:
f.write('调用了batch_input' + '\n')
if __name__ == '__main__':
for i in range(0, 100):
calls = [('wait_input', [1, 2]), ('batch_input', [3, 4])]
choice = random.choice(calls)
this_module = sys.modules[__name__]
getattr(this_module, choice[0])(*choice[1])
关键词:python how to call the different methods randomly
https://stackoverflow.com/questions/53187651/how-to-randomly-choose-method-from-a-list-of-methods-with-different-arguments
https://stackoverflow.com/questions/4131864/use-a-string-to-call-function-in-python