简体   繁体   中英

How can I figure out which parameters map to which variables?

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.

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.

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

But if i assume that [0,0] corresponds to parentState and newState then how "" is passed as parameter in

This assumption is your problem.

[0,0] is the value of newState and "" is the value of 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.

A very easy way to inspect the state of things is to use 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.

(Pdb) parentState
""
(Pdb) newState
[0,0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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