<p>最简单的方法是:</p>
<pre><code>calculus = {
'1': calculus_1,
'2': calculus_2,
'3': calculus_3,
}
physics = {
'1': physics_1,
'2': physics_2,
'3': physcis_3,
}
def Main():
a = input("Choose equation set. Calculus or Physics: ")
if a == "Calculus":
for item in (calculus.keys()):
# I'm using the __name__ attribute on the function handle
print(item,":",calculus.get(item,'-').__name__)
eq_id = input("Enter desired equation function id: ")
eq_arg = input("Press Enter")
calculus[eq_id](eq_arg)
elif a == "Physics":
for item in (physics.keys()):
print(item,":",physics.get(item,'-').__name__) # i'm using the __name__ attribute on the function handle
ph_id = input("Enter desired equation id: ")
ph_arg = input("Press Enter")
physics[ph_id](ph_arg)
</code></pre>
<p>但我建议,您将数据结构更改为以下内容:</p>
<pre><code>calculus = {
'1': {
'func_name': 'calculus 1',
'func': calculus_1 # where calculus_1 is the function handle
},
'2': {
'func_name': 'calculus 2',
'func': calculus_2 # where calculus_2 is the function handle
},
'3': {
'func_name': 'calculus 3',
'func': calculus_3 # where calculus_3 is the function handle
}
}
</code></pre>
<p>然后访问它,比如</p>
<pre><code>for item in (calculus.keys()):
print(item,":",calculus.get(item,'-')['func_name'])
...
calculus[eq_id]['func'](eq_arg)
</code></pre>