My life would be much easier if I could inspect what variables are assigned to what arguments.
如果我可以检查哪些变量分配给哪些参数,我的生活会容易得多。
In the line:
在线:
self.manager.addState("", [0,0]) # start with 2 empty buckets
The addState
method is defined in manager
class as taking 2 parameters. It is called in the playGame
method. I am having trouble understanding what parameters in the signature map to what arguments in the call.
addState方法在manager类中定义为2个参数。它在playGame方法中调用。我无法理解签名映射中的哪些参数与调用中的参数有关。
def addState (self, parentState, newState) :
"add state if it's new. Remember its parent"
if self.seen.has_key(str(newState)) : return
self.seen[str(newState)] = str(parentState)
self.queue.append (newState)
In the code below, if I assume that newState
corresponds to [0,0]
because it is not a singular value(I'm not sure what ""
maps to) then this code should not run at all.
在下面的代码中,如果我假设newState对应于[0,0],因为它不是一个奇异值(我不确定“映射到”),那么这个代码根本不应该运行。
self.manager.addState("", [0,0]) # start with 2 empty buckets
Question(s):
Is my understanding of this correct?
我对此的理解是否正确?
What is the easiest way to inspect the running state of this so I can verify which parameters map to which arguments?
检查此运行状态的最简单方法是什么,以便我可以验证哪些参数映射到哪些参数?
problem link: http://www.openbookproject.net/py4fun/buckets/buckets.py
问题链接:http://www.openbookproject.net/py4fun/buckets/buckets.py
1 个解决方案
#1
But if i assume that [0,0] corresponds to parentState and newState then how "" is passed as parameter in
但是如果我假设[0,0]对应于parentState和newState那么“”是如何作为参数传递的
This assumption is your problem.
这个假设是你的问题。
[0,0]
is the value of newState
and ""
is the value of parentState
[0,0]是newState的值,“”是parentState的值
In the example you linked the state of the world is represented by a one dimensional array. The beginning state of the world is two buckets represented by [0,0]
and it there is a null
state represented by ""
that only applies at the start of the game.
在您链接的示例中,世界的状态由一维数组表示。世界的开始状态是由[0,0]表示的两个桶,并且存在由“”表示的仅在游戏开始时应用的空状态。
A very easy way to inspect the state of things is to use pdb
检查事物状态的一种非常简单的方法是使用pdb
def addState (self, parentState, newState) :
"add state if it's new. Remember its parent"
# Let's augment this by adding a breakpoint
import pdb; pdb.set_trace()
if self.seen.has_key(str(newState)) : return
self.seen[str(newState)] = str(parentState)
self.queue.append (newState)
#print '--- Adding ', newState
Now when you run this code from the python interpreter pdb
will break on these lines and you can inspect what's going on.
现在,当您从python解释器运行此代码时,pdb将在这些行上断开,您可以检查发生了什么。
(Pdb) parentState
""
(Pdb) newState
[0,0]
#1
But if i assume that [0,0] corresponds to parentState and newState then how "" is passed as parameter in
但是如果我假设[0,0]对应于parentState和newState那么“”是如何作为参数传递的
This assumption is your problem.
这个假设是你的问题。
[0,0]
is the value of newState
and ""
is the value of parentState
[0,0]是newState的值,“”是parentState的值
In the example you linked the state of the world is represented by a one dimensional array. The beginning state of the world is two buckets represented by [0,0]
and it there is a null
state represented by ""
that only applies at the start of the game.
在您链接的示例中,世界的状态由一维数组表示。世界的开始状态是由[0,0]表示的两个桶,并且存在由“”表示的仅在游戏开始时应用的空状态。
A very easy way to inspect the state of things is to use pdb
检查事物状态的一种非常简单的方法是使用pdb
def addState (self, parentState, newState) :
"add state if it's new. Remember its parent"
# Let's augment this by adding a breakpoint
import pdb; pdb.set_trace()
if self.seen.has_key(str(newState)) : return
self.seen[str(newState)] = str(parentState)
self.queue.append (newState)
#print '--- Adding ', newState
Now when you run this code from the python interpreter pdb
will break on these lines and you can inspect what's going on.
现在,当您从python解释器运行此代码时,pdb将在这些行上断开,您可以检查发生了什么。
(Pdb) parentState
""
(Pdb) newState
[0,0]