从函数列表中获取随机函数并调用所选函数

2024-09-30 16:19:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个可以调用的函数,它从函数列表中选择一个随机函数,并调用所选函数。这有可能吗?这是我尝试过的。结果什么也没发生

import random

def ran1(): 
    print("test1")

def ran2(): 
    print("test2") 

def ran3(): 
    print("test3") 

def ran4(): 
    print("test4")

list_functions = [ran1,ran2,ran3,ran4]

def ran_choice():
    random.choice(list_functions)

ran_choice()

Tags: 函数import列表defrandomfunctionslistprint