使用instanciated_object(i)对实例化对象进行双重处理?

时间:2014-02-17 02:22:42

标签: python methods

我正在学习python,我正在尝试通过代码来理解pacman程序。到目前为止一切顺利(感谢StackOverflow社区,他们有时真的很有帮助)。

在查看以下实例化方法时遇到了障碍:

以下是我遇到困难的代码:

ghostType = loadAgent(options.ghost, noKeyboard) ## This line uses the loadAgent function below to load the agents operating the ghost specified by the user through the command line. There are different types of ghosts. One the user specified a ghost agent they want to use all ghost-agents will use the same ghost agent type.
args['ghosts'] = [ghostType( i+1 ) for i in range( options.numGhosts )]

代码中的变量是:

  • options.ghost #Type代理自动操作鬼魂。 这些是由用户在通过游戏开始游戏之前指定的 命令行。不同种类的幽灵代理使鬼魂表现出来 不同。但是一旦选择了幽灵类型,所有幽灵都会被选中 游戏将使用相同的代理类型。

  • options.numGhosts#游戏中的鬼魂数量。这也是
    在开始游戏之前由用户通过命令行指定。

我的问题:

我对ghostType(i + 1)表达式感到困惑。

我知道每个游戏都有几个由幽灵代理人自动操作的幽灵。

  1. 似乎向ghostType变量添加(i + 1)会创建更多相同的变量。 这是对的吗?

  2. 这是否可以作为一般规则应用?意思是,如果我将一个对象实例化为一个变量,我可以通过遵循这个模式在字典中保存这个实例化对象的几个版本吗?

  3. 模式?

      

    variable = method_that_instantiates_an_object

         

    dict ['key'] = [范围(列表)中的i的变量(i + 1)]

    任何帮助都将受到高度赞赏。

    麦克

    为您提供正在发生的事情的完整画面:

    每个ghost代理都是通过下面指定的loadAgent方法初始化的:

    def loadAgent(pacman, nographics):                   
        pythonPathStr = os.path.expandvars("$PYTHONPATH") # Looks through all pythonPath Directories for the right module.
        if pythonPathStr.find(';') == -1:                 ## This 'find' method returns the index position where the seach term was found whithin the string and '-1' if it couldn't be found.
            pythonPathDirs = pythonPathStr.split(':')    
    
        else:
            pythonPathDirs = pythonPathStr.split(';')
        pythonPathDirs.append('.')
    
        for moduleDir in pythonPathDirs:
            if not os.path.isdir(moduleDir): continue     ## the os.path.isdir(path) method returns TRUE if path is an existing directory.
            moduleNames = [f for f in os.listdir(moduleDir) if f.endswith('gents.py')] ##this line basically finds all the module's whose names end with 'gents.py' and loads them into a list called moduleNames.
            print(moduleNames)
            for modulename in moduleNames:
                try:
                    module = __import__(modulename[:-3]) ## X = __import__(‘X’) works like import X, with the difference that you A -pass the module name as a string, and B - explicitly assign it to a variable in your current namespace ### This is a way of importing a module when you don't have the module name yet.
                except ImportError:                      
                    continue
                if pacman in dir(module):                ## This line checks whether the variable 'pacman' which was passed to the loadAgent function is part of the 'module' module wich was just imported. For example the agent LeftTurnAgent is specified in the pacmanAgents.py module. The 'pacman' variable contains which agent type the program is supposed to use. If this type is part of the 'module'-module the function returns the agent and the module-code in the 'module' viariable. 
                   if nographics and modulename == 'keyboardAgents.py': ## This line checks whether the 'nographics variable' was activated by the user in the command line function. Nongraphics is an option the developers of this code introduced to be able to run and analyse 100ds of games at the same time. If that was the case, the user can't be the agent operating the game and the game will raise an exception.
                      raise Exception('Using the keyboard requires graphics (not text display)')
                  return getattr(module, pacman)       ## getattr(module, pacman) is equivalent to module.pacman. This is a way of referencing a function without knowing its name until run-time.For example, if we call the LeftTurnAgent this would be a call to the pacmanAgents.LeftTurnAgent.
         raise Exception('The agent ' + pacman + ' is not specified in any *Agents.py.')
    

0 个答案:

没有答案