-1

What I want to do is to have an function called from an array of function. I did my homework and came across this stackOverflow question. https://stackoverflow.com/questions/30549292/in-python-how-do-i-run-an-array-of-functions

However the code offered in it works for calling all the functions in the array in turn. What I would like to do is to call just one function in the array depending on the array index. What I would call in assembly terms "a vectored jump to a subroutine".

I am using Circuit Python on a RP2040

Here is the code:-

def one():
    print("One")

def two():
    print("Two")

def three():
    print("Three")

arr = (one,two,three)


for fnc in arr:
    fnc()

This runs through each function in turn

However what I would like to do is to call just one function depending on an array index. Something like "call function arr[i]

4
  • 5
    you don't need a loop to do that. just do arr[i]() Commented Feb 21, 2023 at 8:31
  • Thanks that did the trick. I have been trying all sorts of variation of the same idea but was getting the syntax wrong. Commented Feb 21, 2023 at 8:42
  • 1
    your link doesn't direct me to a question, but instead to my homepage Commented Feb 21, 2023 at 8:46
  • 1
    BTW, (one,two,three) is not an array (list). It is a tuple. Quite important difference, even though it works the same in this case. Commented Feb 21, 2023 at 8:48

1 Answer 1

0

A function will be called with just

List[k]()

So just use a for loop if you really need it, or call them individually when needeed.

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

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.