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
#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


Messages In This Thread
Call a Function contained in a Tuple - by SamGer - Jun-14-2019, 06:58 AM
RE: Call a Function contained in a Tuple - by buran - Jun-14-2019, 07:13 AM
RE: Call a Function contained in a Tuple - by snippsat - Jun-14-2019, 12:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 863 May-02-2023, 08:40 AM
Last Post: Gribouillis
  code to send attachments contained on the drive. stefanoste78 1 933 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  Remove an item from a list contained in another item in python CompleteNewb 19 5,969 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Tuple generator, and function/class syntax quazirfan 3 4,027 Aug-10-2021, 09:32 AM
Last Post: buran
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 8,202 Apr-30-2021, 07:37 PM
Last Post: quest
  how to call an object in another function in Maya bstout 0 2,128 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,602 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  How to use a tuple as an argument of a function zarox 5 3,760 Nov-14-2020, 08:02 PM
Last Post: buran
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,937 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Struggling for the past hour to define function and call it back godlyredwall 2 2,287 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