python中的函数存根

时间:2017-08-31 00:49:48

标签: python function stubs

这是我编程的前几节课,我遇到了一个我不太懂的问题。

def defeat_balrog(protagonist):    
    def spawn_balrog():
        """Spawns and returns a stubborn balrog"""
        pass
    def balrog_attack(balrog, person):
        """Returns an attack move from the balrog's repertoire"""
        pass
    cave_balrog = spawn_balrog()
    is_balrog_defeated = False
   yell(protagonist, 'You cannot pass!')
    while not is_balrog_defeated:
        current_attack = balrog_attack(cave_balrog, protagonist)
        if current_attack != None:
            take_defensive_action(protagonist, current_attack)
        yell(protagonist, 'YOU SHALL NOT PASS!')
        take_offensive_action(protagonist, cave_balrog)
        is_balrog_defeated = True
    return True

def take_defensive_action(attacked_entity, attack_move):
    """
    attacked_entity anticipates attack_move and defends himself.
    """
    pass
#my stubs here#
defeat_balrog('gandalf')

我应该确定已经使用过的剩余功能,但是尚未创建存根,并从最后一行填写#my stubs here#。不知道如何开始或继续。

1 个答案:

答案 0 :(得分:0)

存根是一个存在的函数,但是没有定义任何有意义的业务逻辑。例如:

def take_defensive_action(attacked_entity, attack_move):
    pass

注意这里的传递声明?这意味着您已经定义了一个有效的函数,但它什么也没做。

将代码粘贴到PyCharm中,我看到以下功能突出显示在"黄色" (这意味着这些函数名称具有Unresolved reference):

  • yell(protagonist, 'YOU SHALL NOT PASS!')

  • take_offensive_action(protagonist, cave_balrog)

清楚存根的含义,你应该能够相应地定义这些功能,因为它们还没有被定义。以下是yell的示例:

def yell(protagonist, message):
    pass

我把第二个留给你。