简体   繁体   English

如何在python中的元组参数中传递一个函数?

[英]How to pass a function in the tuple argument in python?

How to pass a function in the tuple argument?如何在元组参数中传递函数? *players is a tuple. *players 是一个元组。

this is one function inside a class (TicTacToe)这是一个类中的一个函数 (TicTacToe)

def play_game(self, *players):
        state = self.initial
        while True:
            for player in players:
                move = player(self, state)
                state = self.result(state, move)
                if self.terminal_test(state):
                    self.display(state)
                    return self.utility(state, self.to_move(self.initial))

How can I pass the below function in *players arguments?如何在 *players 参数中传递以下函数?

def alpha_beta_player(game, state):
    return alpha_beta_search(state, game)

My attempt is as follows:我的尝试如下:

ttt = MyTicTacToe()
u = ttt.play_game(alpha_beta_player,alpha_beta_player)
print(u)

Enclose passed tuple in parentheses and unpack with *将传递的元组括在括号中并用 * 解包

ttt = MyTicTacToe()
u = ttt.play_game(*(alpha_beta_player,alpha_beta_player))
print(u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM