#================================================== # 1. Select non-function values: #================================================== location = 'myHome' fileLocation = {'myHome' :path1, 'myOffice' :path2, 'somewhere':path3}[location] #================================================== # 2. Select functions: #================================================== functionName = input('Enter a function name') eval('%s()'% functionName ) #================================================== # 3. Select values with 'range comparisons': #================================================== # # Say, we have a range of values, like: [0,1,2,3]. You want to get a # specific value when x falls into a specific range: # # x<0 : 'return None' # 0<=x<1: 'return 1' # 1<=x<2: 'return 2' # 2<=x<3: 'return 3' # 3<=x : 'return None' # # It is eazy to construct a switch by simply making the above rules # into a dictionary: selector={ x<0 : 'return None', 0<=x<1 : 'return 1', 1<=x<2 : 'return 2', 2<=x<3 : 'return 3', 3<=x : 'return None' }[1] # During the construction of the selector, any given x will turn the # selector into a 2-element dictionary: selector={ 0 : 'return None', 1 : #(return something you want), }[1] # This is very useful in places where a selection is to be made upon any # true/false decision. One more example: selector={ type(x)==str : "it's a str", type(x)==tuple: "it's a tuple", type(x)==dict : "it's a dict" } [1] #================================================== # 4. Select functions with 'range comparisons': #================================================== # # You want to execute a specific function when x falls into a specific range: functionName={ x<0 : 'setup', 0<=x<1: 'loadFiles', 1<=x<2: 'importModules' }[1] eval('%s()'% functionName )