1

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):

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

7

You could do:

for fn in [Func1, Func2, Func3]:
    fn(arg1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.