“自我”作为方法属性

2024-09-29 06:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在尝试自学Python,我正在尝试通过Python程序来学习Python,这基本上是一个pacman游戏。在这样做的同时,我发现下面的类调用包含“self”作为方法属性。你知道吗

game = Game(agents, display, self, catchExceptions=catchExceptions)

背景:

此方法调用是以下函数的一部分:

class ClassicGameRules:
    """
    These game rules manage the control flow of a game, deciding when
    and how the game starts and ends.
    """
    def __init__(self, timeout=30):
        self.timeout = timeout

    def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False):
    [1] agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] 
    [2] initState = GameState() 
    [3] initState.initialize( layout, len(ghostAgents) ) 
    [4] game = Game(agents, display, self, catchExceptions=catchExceptions) 
    [5] game.state = initState 
    [6] self.initialState = initState.deepCopy()

[1]为了使这一点更容易接受,'agents'包含pacman和ghost agent对象,它们组合成一个列表列表,当打印出来时看起来像这样:

[<keyboardAgents.KeyboardAgent instance at 0x1004fe758>, <ghostAgents.RandomGhost instance at 0x1004fe518>, <ghostAgents.RandomGhost instance at 0x1004fe7a0>, <ghostAgents.RandomGhost instance at 0x1004fe950>, <ghostAgents.RandomGhost instance at 0x1004feb90>]

[2]initState=GameState():GameState()是一个数据对象,包含有关游戏状态的各种信息

[3条]初始化状态.initialize(layout,len(ghostAgents)):初始化布局数组中的第一个游戏状态。这个布局数组让程序知道吃豆人和幽灵在哪里产卵,食物和胶囊在哪里展示,墙壁在哪里可以找到。你知道吗

[4]game=game(agents,display,self,catchExceptions=catchExceptions):game类管理控制流,从代理请求操作。代理可以是键盘代理(由控制吃豆人的玩家控制)和控制鬼魂的自动代理(见[1])。你知道吗

[5条]游戏状态=initState:将[2]的内容复制到变量中游戏状态. 你知道吗

[6条]自身初始状态= initState.deepCopy文件():deepCopy是一个函数,它对定义游戏板游戏状态的变量(如食物的布局或位置)执行复制操作。你知道吗

班级游戏: 下面是班级游戏的代码:

class Game:
"""
The Game manages the control flow, soliciting actions from agents. 
"""

def __init__( self, agents, display, rules, startingIndex=0, muteAgents=False, catchExceptions=False ):
    self.agentCrashed = False
    self.agents = agents                                           ## agents contain the pacman and ghost agent objects combigned into a list of lists agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()]
    self.display = display                                        
    #print("This is the display object" + str(self.display))
    self.rules = rules                                             ## The rules are passed on as the self attribute of the ClassicGameRules class within the call of Game in newGame [REF 115].
    self.startingIndex = startingIndex                             ## starting index is given as default attribute by the __init__ method.
    self.gameOver = False
    self.muteAgents = muteAgents
    self.catchExceptions = catchExceptions
    self.moveHistory = []
    self.totalAgentTimes = [0 for agent in agents]                 ## This creates a list that replaces the list elements of the agents list, which contains the agent objects, with zeros. It looks something like this [0,0,0]
    ##print(self.totalAgentTimes)
    self.totalAgentTimeWarnings = [0 for agent in agents]          
    self.agentTimeout = False
    import cStringIO                                              
    self.agentOutput = [cStringIO.StringIO() for agent in agents] 

什么使我困惑

当调用game=game(agents,display,self,catchExceptions=catchExceptions)时,“self”作为一个属性传递给game。在游戏中,“self”takes中包含的数据放在“rules”变量中。你知道吗

但是这个变量包含什么呢?你知道吗

直观地说,我建议它应该是newGame对象的“self”实例。但是看看这个方法是怎么调用的自身初始状态= initState.deepCopy文件(),这似乎不合情理。。。你知道吗

或者我们是在引用ClassicGameRules类的self对象?你知道吗

我知道这是一个少数文本阅读,但我不太确定如何缩短这个问题。如果需要更多信息,请告诉我。任何帮助都将不胜感激。:)

佐乌101


Tags: oftheselfgamefalse游戏状态display
2条回答

线路:

game = Game(agents, display, self, catchExceptions=catchExceptions) 

ClassicGameRules.newGame中,使用三个位置参数和一个关键字参数以及新Game实例的隐式第一个位置参数调用Game.__init__Game.__init__定义为:

def __init__(self, agents, display, rules, startingIndex=0, muteAgents=False, catchExceptions=False ): 
                 # agents  display  self   [default]        [default]         catchExceptions

关键的是,调用函数中的self与被调用函数中的self不同。此调用将ClassicGameRules实例作为参数rules传递给Game实例。这将创建一个双向链接:

  • 您可以通过self.game访问ClassicGameRules实例方法中的Game实例;以及
  • 您可以通过self.rules访问Game实例方法中的ClassicGameRules实例。你知道吗

下一部分:

game.state = initState 
self.initialState = initState.deepCopy()

Game实例的state属性设置为initState,然后将该状态的副本保存为self.initialState(其中self指代ClassicGameRules实例,因为我们在ClassicGameRules实例方法中)。你知道吗

在类ClassicGameRules的方法newGame中,self表示类的当前实例:

game = Game(       agents, display, self, ...) 
                      v       v       v
def __init__(self, agents, display, rules, ...)

这里__init__方法的第一个self表示将在Game类中创建的新对象。你知道吗

在您的例子中,Game类在rules中有一个对ClassicGameRules实例的引用,该实例调用了Game实例的构造函数。deepCopyafter与此无关,只是通过将刚刚创建的对象(game)的状态映射到“parent”对象(实例ClassicGameRules)来同步两个对象的初始状态。你知道吗

相关问题 更多 >