Python Forum
Is there a something like "case" in Python 3.6?
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a something like "case" in Python 3.6?
#3
In python we switch on dictionaries.

A brief (and silly) example:
def func_one():
    print("Hey from func one.")

def func_two():
    print("Hey from func two.")

def default():
    print("I'm some default func.")


SWITCH_DICT = {".3" : func_one, ".4" : func_two}


user_input = input("Enter .3 or .4: ")
SWITCH_DICT.get(user_input, default)()
Output:
Enter .3 or .4: .3 Hey from func one.
Output:
Enter .3 or .4: .4 Hey from func two.
Output:
Enter .3 or .4: spam I'm some default func.
This technique is extremely common and useful in a wide variety of situations.
Reply


Messages In This Thread
RE: Is there a something like "case" in Python 3.6? - by Mekire - Feb-24-2017, 08:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What colon (:) in Python mean in this case? Yapwc 4 2,312 Dec-28-2022, 04:04 PM
Last Post: snippsat
  Switch case or match case? Frankduc 9 4,812 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,717 Jul-27-2021, 02:02 PM
Last Post: Suriya
  Help: write 'case' with Python ICanIBB 2 1,933 Jan-27-2021, 09:39 PM
Last Post: Larz60+
  How to use switch/case in python? newbieguy 9 4,244 Nov-08-2019, 11:35 AM
Last Post: newbieguy
  How to write switch case statement in Python pyhelp 9 9,465 Nov-11-2018, 08:53 PM
Last Post: woooee

Forum Jump:

User Panel Messages

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