我有一個寫了一個簡單class
與__init__
模擬開關/箱流量:Python字典查找返回多個結果?
class Foo(object):
def bar(self):
print "hello bar"
def haz(self):
print "hello haz"
def nothing(self):
print "None"
def __init__(self, choose_me):
{'foo': self.bar(),
'can': self.haz()
}.get(choose_me, self.nothing())
if __name__ == '__main__':
Foo('foo')
爲什麼一切都被選中? - 這是它給我的輸出(run it with ideone):
招呼吧
你好HAZ
無
或者,你可以添加'()'在字典的末尾(就像我的回答)... –