Python Forum
Call a Function contained in a Tuple
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a Function contained in a Tuple
#1
Hallo Users,

I have a Tuple of Variables and corresponding to that a Tuple of functions. Now based on what variable is used a function needs to be called. Seeking help on calling the function from Tuple of function

Var_Tuple=('Var1','Var2','Var3')
Func_Tuple=('Func1()','Func2()','Func3()')

So if the user Types Var3 then the Func3() must be called.

Best Regards
Reply
#2
def spam():
    print('spam')
    
def eggs():
    print('eggs')
    
funcs = {'foo':spam, 'bar':eggs}

user_choice=input('foo or bar?')
funcs[user_choice]()
Output:
foo or bar?bar eggs >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi,

 Func_Tuple=('Func1()','Func2()','Func3()')
is _not_ a tuple containing functions, but a tuple containing strings.
Except this, having a varible name suffix stating the data structure tye doesn't make to much sense, as the data type is clearly visible from the code. Having this suffix is not really good style, so try to get rid of it as soon as you can.

As @buran showed, use a data structure with a mapping, with is a Python dict. That's the better way.

Regards, noisefloor
Reply
#4
Thank you for your valuable suggestions
Reply
#5
Can also show a way that i like better.
funcs[user_choice]() as @buran show have done many times,but have tired to go away for using it.
I think looking at () after a list can in some case be confusing,can also be using get() to do error checking.
Then it look like this.
def spam():
    return('spam')

def eggs():
    return('eggs')

def switch_case(user_choice):
    return {
        'foo': spam(),
        'bar': eggs(),
    }.get(user_choice, f'<{user_choice}> Not in Record')

user_choice = input("foo or bar? ")
print(switch_case(user_choice))
Output:
foo or bar? foo spam foo or bar? bar eggs foo or bar? car <car> Not in Record
Reply
#6
@snippsat return is not a function so no need to use brackets
return 'spam' works fine. :-)
Reply
#7
(Jun-14-2019, 04:18 PM)ThomasL Wrote: @snippsat return is not a function so no need to use brackets
return 'spam' works fine. :-)
Thanks,did a rewrite of first example which had print('spam') then i just changed print with return,and did forget to remove ().
Reply
#8
We are all humans ;-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 787 May-02-2023, 08:40 AM
Last Post: Gribouillis
  code to send attachments contained on the drive. stefanoste78 1 848 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  Remove an item from a list contained in another item in python CompleteNewb 19 5,661 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Tuple generator, and function/class syntax quazirfan 3 3,854 Aug-10-2021, 09:32 AM
Last Post: buran
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,051 Apr-30-2021, 07:37 PM
Last Post: quest
  how to call an object in another function in Maya bstout 0 2,070 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,461 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  How to use a tuple as an argument of a function zarox 5 3,565 Nov-14-2020, 08:02 PM
Last Post: buran
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,795 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Struggling for the past hour to define function and call it back godlyredwall 2 2,202 Oct-29-2020, 02:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020