You could use the usual Python way to avoid the switch
statement used in other languages: the dictionary. Functions are object, so a function can be a value in a dictionary.
function_dict = {'X': foo.X, 'Y': foo:Y, 'Z': foo.Z}
return function_dict[val](self)
If val
is not one of the keys in the dictionary, an error will be raised. If you do not want that particular error, there are several ways to raise another error or avoid errors altogether. For example, you could use the get
method of dictionaries to use a default value if the key is not in the dictionary. For example, if you want the method foo.other
to be called for any key other than X, Y, Z
you could use
function_dict = {'X': foo.X, 'Y': foo:Y, 'Z': foo.Z}
return function_dict.get(val, other)(self)
You could also check before using the dictionary if val
is one of the keys, as in
function_dict = {'X': foo.X, 'Y': foo:Y, 'Z': foo.Z}
if val not in function_dict:
sys.exit("ERROR: value %s DNE"%(val))
return function_dict[val](self)