Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

python - how to access the list in different function

I have made a class in which there are 3 functions.

  1. def maxvalue
  2. def min value
  3. def getAction

In the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction function so that I can reverse the list and then take out the first entry from it. How can do i that??

 def getAction(self,gamestate):
      bestaction.reverse()
      return bestaction[0]




 def maxvalue(gameState, depth):

    actions = gameState.getLegalActions(0);
    v = -9999
    bestaction = []
    for action in actions:
      marks = minvalue(gameState.generateSuccessor(0, action), depth, 1)
      if(v < marks):
       v = marks
       bestaction.append(action)
    return v

It is giving me an error....."global name bestaction is not defined"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's a good idea to post actual code - it makes it easier to see what's happening.

With that said, you probably want something like this:

class MyClass(object):
    def max_value(self):
        # assigning your list like this will make it accessible 
        # from other methods in the class
        self.list_in_max_value = ["A", "B", "C"]

    def get_action(self):
        # here, we're doing something with the list
        self.list_in_max_value.reverse()
        return self.list_in_max_value[0]

>>> my_class = MyClass()
>>> my_class.max_value()
>>> my_class.get_action()
"C"

You might want to read through the python class tutorial.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...