1

这是我的第一篇文章,所以请放轻松。当然,这不太容易;)

反正我是用 Python 3.2.3 写一个文字冒险游戏。有一个类 Labyrinth 包含 Location 对象的集合,这些对象又包含 Actor 的 GameItem 的子类。

在一个回合中,Labyrinth 轮询每个位置,轮询其内容,以获取由 Labyrinth.doTurn() 执行的解析器命令对象。命令执行通常会导致执行对象的操作回调返回 Message 的子类。

无论如何,下面的代码是来自 Labyrinth 的相关片段。它在“结果”变量中检索消息:

 for command in self._pollLocations():
        if command.getVerb() == 'quit':
            if self._confirmQuit() is True:
                return 2  # status 2 == quit w/o win or loss    
            else:
                print( "Whew! I sure am glad for that!" )
        else:
            print( "^BEFORE" )
            print( "command: %s\tperformer: %s" % ( command.getVerb(), command.performer.getName() ) )
            result = command.performer.execute( command )
            print( "vAFTER"  )
            print( "Got result: %s" % str(result) )

        if result is None:
            print( "Got NONE: returning it" )
            return None

        if isinstance( result, Exception ):
            raise( result )

        if isinstance( result, Message ):
            print( "Found Message instance:" )
            result.write()
            return None

但是,无论我在被调用方法的 return 语句中返回什么,上面的result =调用总是得到 None。这是我正在调用的 Actor.take() 方法:

  def take( self, arg ):
    """
    Take an item in an accessible area.
    """
    item = self.location.getItemByName( arg )
    if isinstance( item, MatchNotFound ):
        print( "Actor.take(): Returning: %s" % str( ThatsNotHereMsg() ) )
        return ThatsNotHereMsg()

    elif isinstance( item, MissingDirectObject ):
        print( "Missing DO" )
        return TellMeWhatToTakeMsg()
    elif isinstance( item, AlgorithmCouldNotDecide):
        print( "Algorthm coul dnot decide." )
        return IndecisionMsg()
    elif isinstance( item, YouShouldNotBeSeeingThisError ):
        print( "Returning GenericGameMsg" )
        return GenericGameMsg( "You should not be seeing this error. "   \
               "The programmer has made a serious mistake "              \
               "and should be beaten for it. Besides, he's "             \
               "a lazy bum who never delivers software on time."         \
        )
    else:
        print( "take: made it past if..elif chain" )

    if item is None:
        print( "item is None: returning TellMeWhatToMakeMsg()" )
        return TellMeWhatToTakeMsg()

    if item.isTakeable( by=self ) is True:
        print( "item.isTakeable: performing take" )
        self.addItem( item )
        self.location.delItem( item )
        return YouTookItMsg()
    print( "take: got to the end. Returning YouJustCantHaveItMsg" )
    return YouJustCantHaveItMsg()

最后,这是我运行游戏时的输出。所有这些打印语句都用于调试。你会看到我试图获取一个不存在的项目来导致 MatchNotFound,它应该返回 ThatsNotHere 的一个新实例,但没有。

You are in the cavern zone of a natural cave in the East Texas countryside.
You know this cave is unexplored. The deep, dark tunnel lies to the north.
"There's really no turning back now!", you tell yourself.
There is: a shady  character (i.e. you) here.


t1 CavernZone ~> take gold coin 
^BEFORE
command: take   performer: player
Actor.take(): Returning: You don't see that here.
vAFTER
Got result: None
Got NONE: returning it

t2 CavernZone ~>

所以......我的问题是,这里发生了什么?我的新实例是否因为函数在返回值分配给调用者之前返回而被 GC 处理?或者是其他东西。我真的不知道,任何帮助将不胜感激。

谢谢,彼得

编辑:

正如 Roland 下面所说,发布(并检查!) performer.execute() 代码。原来我忘了在那个函数中返回结果!谢谢你的额头,原来我需要很多!;)

def execute( self, cmd ):
    if cmd.callback is not None:
        result = eval( cmd.callback )( cmd.getArgs() ) 
    else:
        pass

它应该是:

def execute( self, cmd ):
    if cmd.callback is not None:
        result = eval( cmd.callback )( cmd.getArgs() ) 
    else:
        result = None
    return result

所以,再次感谢您的帮助!

以下是按计划运行的程序的输出:

t14 CavePassageOne ~> take something
Yes, but what?

t15 CavePassageOne ~> take computer
You don't see that here.
4

0 回答 0