擅长:python、mysql、java
<p>在这种情况下,您应该使用<code>dictionary</code></p>
<p>例如:</p>
<pre><code>CONST1 = 'CONST1'
CONST2 = 'CONST2'
def f1():
return 'Hello from f1 function'
def f2():
return "Hello from f2 function"
my_data = {
'CONST1': f1,
'CONST2': f2,
}
def run(key):
# If there is no key, just return message
# Or use try/catch
if key not in my_data.keys():
return 'Oops, I can\'t find the key'
# Here we get a value via key, and call function via parentheses
return my_data[key]()
print(run('CONST1'))
</code></pre>