简体   繁体   中英

How to call random function from list of functions?

t = 1
while t < total:
    random.choice([xs_tables(t), medium_tables(t), large_tables(t), xl_tables(t)])
    t = t + total

Above I"m trying to call a random function from a list of 4 functions that will take t as a input. Right now the code is calling each function in the list instead of just one random function. I think it might have something to do with the ( t ) but I'm not sure how else to pass the functions t .

Do this instead:

random.choice([xs_tables, medium_tables, large_tables, xl_tables])(t)

This way, you first select the function and then call it with the argument t .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM