英文:
Passing self from a member function to a function in another class
问题
为了上下文,我正在尝试在Python中实现一个极小化极大算法来玩井字游戏。以下是相关的代码:
TicTacToe.py
def dumbPlayer(game, gameType):
return game.successors()[0]
class minimax:
tree = None
def minimaxPlayer(self, game, gameType):
return game.successors()[0]
class TicTacToe:
def play(self, xPlayer=consolePlayer, oPlayer=consolePlayer, output=consolePrint):
while self.winner is None:
output(self)
if self.turn == 'X': self.move(xPlayer(self, TicTacToe))
elif self.turn == 'O': self.move(oPlayer(self, TicTacToe))
output(self, True)
return
main.py:
import TicTacToe as TTT
player2 = TTT.minimax.minimaxPlayer
test = TTT.TicTacToe()
test.play(TTT.dumbPlayer, player2)
传递dumbPlayer函数是正常的,问题出在minimaxPlayer函数上。当play函数调用minimaxPlayer函数时,我收到以下错误:
TypeError: minimaxPlayer()缺少一个必需的位置参数:'gameType'
根据我对Python的理解,"self"参数会自动传递给函数,并且不能显式传递(或者可能不能显式传递)。所以在play函数看来,
def dumbPlayer(game, gameType)
和
def minimaxPlayer(self, game, gameType)
应该是等价的,但在这种情况下似乎不正确。
是否有一个简单的修复方法?还是我需要重新构建我的代码?如果必须的话,我已经有一些关于如何重构代码的想法,但我宁愿不这样做,并且我想知道为什么这不按照我期望的方式工作的根本原因。
英文:
For context, I am trying to implement a minimax algorithm in python to play tic tac toe. Here's the relevant code:
TicTacToe.py
def dumbPlayer(game, gameType):
return game.successors()[0]
class minimax:
tree = None
def minimaxPlayer(self, game, gameType):
return game.successors()[0]
class TicTacToe:
def play(self, xPlayer=consolePlayer, oPlayer=consolePlayer, output=consolePrint):
while self.winner is None:
output(self)
if self.turn == 'X': self.move(xPlayer(self, TicTacToe))
elif self.turn == 'O': self.move(oPlayer(self, TicTacToe))
output(self, True)
return
main.py:
import TicTacToe as TTT
player2 = TTT.minimax.minimaxPlayer
test = TTT.TicTacToe()
test.play(TTT.dumbPlayer, player2)
Passing in the dumbPlayer function works fine, the problem comes with the minimaxPlayer function. When the play function calls the minimaxPlayer function, I get the following error:
TypeError: minimaxPlayer() missing 1 required positional argument: 'gameType'
From what I understand of python, the "self" parameter is automatically passed to the function and doesn't (or maybe can't) be passed explicitly, so as far as the play function is concerned,
def dumbPlayer(game, gameType)
and
def minimaxPlayer(self, game, gameType)
should be equivalent, but it appears that's not correct in this case.
Is there an easy fix for this? Or will I need to refactor my code? I already have some ideas for how I can restructure the code if I have to, but I'd rather not, and I'd like to know the underlying reason for why this doesn't work how I expect.
答案1
得分: 1
对于实例方法的`self`参数,会自动绑定到你从中获取该方法的实例上,但你正在访问*类*的版本该方法。
而不是:
```python
player2 = TTT.minimax.minimaxPlayer
请执行以下操作:
minimax_instance = TTT.minimax()
player2 = minimax_instance.minimaxPlayer
它应该按照你的要求工作。
注意:在Python中的标准约定是使用CamelCase作为类名,使用snake_case作为实例名,这样就不需要使用像minimax_instance
这样奇怪的名称来区分类和实例了。你可能还希望tree
是一个实例属性,而不是一个类属性(如果想要能够将Minimax类与不同的游戏一起使用,你不希望不同的实例互相干扰状态)...
class Minimax:
def __init__(self):
self.tree = None
def player(self, game, game_type):
return game.successors()[0]
minimax = TTT.Minimax()
player2 = minimax.player
<details>
<summary>英文:</summary>
The `self` argument for an instance method is automatically bound to the instance that you got the method from, but you're accessing the *class*'s version of that method.
Instead of:
player2 = TTT.minimax.minimaxPlayer
do:
minimax_instance = TTT.minimax()
player2 = minimax_instance.minimaxPlayer
and it should behave the way you want.
Note: the standard convention in Python is to use CamelCase for class names and snake_case for instance names, which eliminates the need to use weird names like `minimax_instance` to differentiate between the class and the instance. You probably also want `tree` to be an instance attribute rather than a class attribute (if the idea is to be able to use the Minimax class with different games, you don't want the different instances stepping on each others' state)...
class Minimax:
def init(self):
self.tree = None
def player(self, game, game_type):
return game.successors()[0]
minimax = TTT.Minimax()
player2 = minimax.player
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论