2017-04-19 22 views
1

所以我試圖找出如何使代碼工作的這一塊,特別是內,如果我遍歷每個功能「功能」我怎麼才能調用這個函數。如何遍歷的函數列表,並調用這些功能的循環

def compare_two_hands(h1, h2): 
    determinants = [is_flush(h), is_two_pair(h), is_one_pair(h)] 
    for func in determinants: 
     if func(h1) or func(h2): 
      if func(h1) and func(h2): 
       ... 
      else: 
       ... 

回答

1

您的調用的功能,當你寫它的代碼,將工作。唯一的問題是你如何定義你的名單determinants。我假設這三個函數是在同一個命名空間的其他地方定義的。當您在構建列表時參考它們時,只需丟失(h)

def is_flush(h): 
    ... 

def is_two_pair(h): 
    ... 

def is_one_pair(h): 
    ... 

def compare_two_hands(h1, h2): 
    determinants = [is_flush, is_two_pair, is_one_pair] 
    # rest of function as you already have it