【问题标题】:How do I iterate over a list of functions and call those functions within the loop如何遍历函数列表并在循环中调用这些函数
【发布时间】:2017-04-19 00:14:12
【问题描述】:

所以我试图弄清楚如何使这段代码工作,特别是,如果我要迭代每个函数“func”,我该如何调用该函数。

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:
                ...

【问题讨论】:

    标签: python-3.x iteration function-call


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多