Candyjet
Posts: 56
Joined: Sun Jan 10, 2016 3:45 pm

Using tuple/lists and functions

Tue Mar 01, 2016 12:15 pm

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

DirkS
Posts: 10018
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using tuple/lists and functions

Tue Mar 01, 2016 12:24 pm

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]() )

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Using tuple/lists and functions

Tue Mar 01, 2016 12:42 pm

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]()
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Using tuple/lists and functions

Tue Mar 01, 2016 2:27 pm

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]()

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Using tuple/lists and functions

Tue Mar 01, 2016 4:37 pm

If you want to see the name of the function then you can use

Code: Select all

print(acttup[0].__name__)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Candyjet
Posts: 56
Joined: Sun Jan 10, 2016 3:45 pm

Re: Using tuple/lists and functions

Tue Mar 01, 2016 5:07 pm

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

Candyjet
Posts: 56
Joined: Sun Jan 10, 2016 3:45 pm

SOLVED Re: Using tuple/lists and functions

Wed Mar 02, 2016 11:18 am

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

Return to “Python”