Home > Mobile >  Python List of Functions without Executing
Python List of Functions without Executing

Time:10-26

I'm working on a Python project, and part of it is where it chooses a function from random via list. Unfortunately, when it's declaring the list, its executing the function. Is there a way to have a list of functions without them executing?

Here's example code:

import random
def a():
  global var
  var = 1
def b():
  global var
  var = 2
list = [a(), b()]
print(var)

Outcome:

>>> 2

CodePudding user response:

You need to store the functions themselves, not the results of calling them.

list = [a, b]
  • Related