0

If I have different functions with increasing numbers in their names how do I loop through them? For example:

def Func1():
    something something

def Func2():
    something something

def Func3():
    something something

can I loop through them with a:

for i in range(1,4):
user3874806
  • 13
  • 1
  • 4

2 Answers2

8

You can put the functions in a list and loop over that:

for func in [Func1, Func2, Func3]:
    result = func()

Functions are first-class objects in Python, you can create (additional) references to them and call them from whatever reference you have.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
7

You could do:

for fn in [Func1, Func2, Func3]:
    fn(arg1)
mdml
  • 19,981
  • 8
  • 48
  • 61