Python Alternative For Switch Statements

Do you like this?

Summary:
This snippet shows you one of the approaches to implement switch statements in python.


Content:

def Method1(argument):
    pass

def Method2(argument):
    pass

print "1. Option 1"
print "2. Option 2"
options= {
    '1': Method1,
    '2': Method2,
    }
option= raw_input("Option:")
argument = raw_input("Argument:")
if option in options.keys():
    options[option](argument)
else:
    exit(1)

 
comments powered by Disqus