我现在正在尝试自学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
线路:
在
ClassicGameRules.newGame
中,使用三个位置参数和一个关键字参数以及新Game
实例的隐式第一个位置参数调用Game.__init__
。Game.__init__
定义为:关键的是,调用函数中的
self
与被调用函数中的self
不同。此调用将ClassicGameRules
实例作为参数rules
传递给Game
实例。这将创建一个双向链接:self.game
访问ClassicGameRules
实例方法中的Game
实例;以及self.rules
访问Game
实例方法中的ClassicGameRules
实例。你知道吗下一部分:
将
Game
实例的state
属性设置为initState
,然后将该状态的副本保存为self.initialState
(其中self
指代ClassicGameRules
实例,因为我们在ClassicGameRules
实例方法中)。你知道吗在类
ClassicGameRules
的方法newGame
中,self
表示类的当前实例:这里
__init__
方法的第一个self
表示将在Game
类中创建的新对象。你知道吗在您的例子中,
Game
类在rules
中有一个对ClassicGameRules
实例的引用,该实例调用了Game
实例的构造函数。deepCopy
after与此无关,只是通过将刚刚创建的对象(game
)的状态映射到“parent”对象(实例ClassicGameRules
)来同步两个对象的初始状态。你知道吗相关问题 更多 >
编程相关推荐