尽管显式返回了新的类实例,但Python3方法调用返回None

时间:2012-10-28 00:41:44

标签: python python-3.x garbage-collection

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

无论如何,我正在使用Python 3.2.3编写一个文本冒险游戏。有一个Labyrinth类,它包含一个Location对象的集合,而这些对象又包含Actor的GameItem的子类。

在转弯过程中,Labyrinth会轮询每个轮询其内容的位置,以查找由Labyrinth.doTurn()执行的解析器Command对象。命令执行通常会导致执行对象的操作回调返回Message的子类。

无论如何,以下代码是Labyrinth的相关代码段。它在'result'变量中检索Message:

 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.

0 个答案:

没有答案
相关问题