[Solved] Is is safe to use dictionary class as switch case for python?


Yes the dictionary will be re-created if you leave and then re-enter that scope, for example

def switch(a):
    responses = {1: 'a',
                 2: 'b',
                 3: 'c'}
    print(id(responses))
    return responses[a]

Notice that the id of responses keeps changing which illustrates that a new object has been created each time

>>> switch(1)
56143240
'a'
>>> switch(2)
56143112
'b'
>>> switch(3)
56554184
'c'

3

solved Is is safe to use dictionary class as switch case for python?