我想測試使用字典來調用函數的概念,因爲python沒有case switch
,我不想寫出一大堆if
語句。然而,每當我試圖把功能的字典,我得到如下:函數字典的語法?
def hello():
... print 'hello world'
...
>>> fundict = {'hello':hello()}
hello world
>>> fundict
{'hello': None}
>>> fundict = {'hello':hello}
>>> fundict['hello']
<function hello at 0x7fa539a87578>
我怎麼叫fundict
所以叫時運行hello()
?我研究了一些其他的堆棧問題,但我沒有掌握語法,或者可能不理解它在給我一個地址。
這兩個答案都很好,很具描述性。 – JFA