-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.py
More file actions
103 lines (50 loc) · 1.48 KB
/
Copy pathSwitch.py
File metadata and controls
103 lines (50 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#Python Dictionaries: Pointers, Switch, Overloading Functions File0
#Goal:
a = 1
b = a
a = 2
print(b)
# output: 2
# Actual output: 1
# Way to Do it:
a = {}
a[0] = 1
b = a
a[0] = 2
print(b[0])
# output: 2
# all sorts of things can go into the key and value of a dictionary. like Functions.:
cat,dog = 1,2
def GetCat(): return cat
def GetDog(): return dog
a[GetCat]=GetDog
# notice we do not use the () because we want the Function it's self not the result of the function
print(a[GetCat]() )
#output: 2 # because it ran dog.
# We can use this to fake a switch case statement:
def CaseA(): print("""Case A""")
def CaseB(): print("""Case B""")
def CaseC(): print("""Case C""")
def CaseDefault(): print("""default""")
switch ={"a":CaseA,"A":CaseA,"b":CaseA,"B":CaseA,"c":CaseA,"C":CaseA}
def TheSwitchCase(Check):
if Check in switch:
return switch[Check]()
else:
return CaseDefault()
TheSwitchCase("A")
# output: Case A
TheSwitchCase("a")
# output: Case A
TheSwitchCase("k")
# output: default
# You can change the case at run-time, for WowCool or OhThatsbad.
def WowCool(): print("""Wow Cool""")
def OhThatsbad(): print("""Oh That's bad""")
switch["A"]=WowCool
TheSwitchCase("A")
# output: Wow Cool
switch["b"]=OhThatsbad
TheSwitchCase("b")
# output: Oh That's bad
# let's make a new file and the switch with arguments to it's functions.