Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose a random method from a class

I have a class with some functions, like this:

class valami:

    def a():
        *some code...*
    def b():
        *some code...*
    def c():
        *some code...*

I want to choose a random function from the valami calss. I already tried this:

random.choice((valami.a(), valami.b(), valami.c()))

But it called all the functions at same time.

like image 283
Bálint Cséfalvay Avatar asked Apr 19 '26 16:04

Bálint Cséfalvay


1 Answers

Don't call them right away, only call the function that the code randomly chose:

random.choice((valami.a, valami.b, valami.c))()

If you don't want to call it yet, just remove the pair of parenthesis at the end, make it become:

random.choice((valami.a, valami.b, valami.c))
like image 175
U12-Forward Avatar answered Apr 22 '26 16:04

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!