Page 1 of 1

Using tuple/lists and functions

Posted: Tue Mar 01, 2016 12:15 pm
by Candyjet
I am trying to write some python3 code to do the following. There will be seven different functions defined. User input will define multiple numerical entries of numbers between 1 and 7. This will be of varying length, so for example, I would end up with a list of numbers between 1 and 7 - 1,4,2,3,5,6,1,2.... and so on.
From this list, I want to call a corresponding function, so the first number 1 would call function1, then the 4, function4 etc.

The list of functions will be fixed at seven, so I thought about using tuples to define the list of functions, then call the function from the tuple.

I didn't get very far.

Code: Select all

def action0():
    print ('this is action0')
    
def action1():
    print ('this is action1')

acttup=action0(),action1()   

print (acttup[0])
acctup is my tuple. I was hoping to see 'action0()' appear when the code is run, but instead I get 'None'

A couple of questions.
Why do I get None, from the print command?
Looking at the bigger picture, is this the best/easiest method for what I want to achieve?

Thanks,

CJ

Re: Using tuple/lists and functions

Posted: Tue Mar 01, 2016 12:24 pm
by DirkS

Code: Select all

acttup=action0(),action1()   
action0() will call the function. You want to get a reference:

Code: Select all

acttup=action0,action1
Not sure how to call these functions, though.
Maybe

Code: Select all

print( acctup[0]() )

Re: Using tuple/lists and functions

Posted: Tue Mar 01, 2016 12:42 pm
by elParaguayo
Not sure how to call these functions, though.
Maybe

Code: Select all

print( acctup[0]() )
Essentially, you're absolutely right. However, in this particular example, given that the function is just a print statement, there's no return value, so running this would output:

Code: Select all

this is action0
None
So it would be better to just call it like this:

Code: Select all

acctup[0]()

Re: Using tuple/lists and functions

Posted: Tue Mar 01, 2016 2:27 pm
by RogerW
Would this do (data 1-3)

Code: Select all

def fna():
    print('a')
    
def fnb():
    print('b')
    
def fnc():
    print('c')

fns = [fna,fnb,fnc]
data = [1,3,2,2,1]

for i in data:
    fns[i-1]()

Re: Using tuple/lists and functions

Posted: Tue Mar 01, 2016 4:37 pm
by scotty101
If you want to see the name of the function then you can use

Code: Select all

print(acttup[0].__name__)

Re: Using tuple/lists and functions

Posted: Tue Mar 01, 2016 5:07 pm
by Candyjet
Thanks very much for your efforts. I should have made clear that the Print line was just there to show what the content of acttup was, before trying anything more complex with it.

I think tomorrow is now written off.

Thanks again. :D

SOLVED Re: Using tuple/lists and functions

Posted: Wed Mar 02, 2016 11:18 am
by Candyjet
Have just taken up where I left off yesterday, and it now all works. H/T to RogerW, who's code does exactly what it says on the tin.
Am now combining all the suggestions, to get exactly what I want.

Thanks very much,

A happy CJ