首页 > 解决方案 > 调用方法作为参数

问题描述

谈到 Python 和第一次在这里发帖时,我完全是新手(尽管到目前为止我已经大量使用了帮助页面)——如果我忽略了任何明显的东西,请原谅我。

我正在尝试将类中的函数用作类外函数中的参数,但它不起作用。这是我的代码...

# These are the attributes I use to build my fighter class and their various attack moves.

combat_system.py

Class Attack:
    def __init__(self, name, ST, QU, FL):
        self.name = name       
        self.SP = SP
        self.ST = ST
        self.QU = QU
        self.FL = FL

    def punch(self):
        return int(self.ST + self.QU)

    def kick(self):
        return int(self.ST + self.FL)

# Class instances are built from a dict
Rex = {'name': Rex, 'ST': 5, 'QU': 8, 'FL': 7}
Mac = {'name': Mac, 'ST': 9, 'QU': 3, 'FL': 3}

#And class instances are established in the game.py file

game.py
attacker1 = Attack(Rex['name'], Rex['ST']...
attacker2 = Attack(Mac['name'], Mac['ST']...

当我这样做时,我得到了想要的结果......注意这个函数存在于攻击类之外。(attacker1、attacker2、defender1、defender2 都是 Attack Class 实例)

combat_system.py

def battle():
    attack1_result = attacker1.punch() - defender1.punch() 
    attack2_result = attacker2.kick() - defender2.kick()
    return attack1_result, attack2_result

但我想要做的是让游戏玩家选择每个攻击者执行的移动并将该攻击类功能传递给战斗功能

# gamer makes a choice for each attacker

game.py

    xy = battle(punch, kick)

我在战斗功能中编写代码如下......

combat_system.py

    def battle(attack1_move, attack2_move):
        attack1_result = attacker1.attack1_move() - defender1.attack1_move() 
        attack2_result = attacker2.attack2_move() - defender2.attack2_move()
        return attack1_result, attack2_result

我收到以下错误:

NameError:未定义名称“punch”

当我尝试这个时-

game.py

    xy = battle(Attack.punch, Attack.kick)

combat_system.py

    def battle(attack1_move, attack2_move):
        attack1_result = attacker1.attack1_move() - defender1.attack1_move() 
        attack2_result = attacker2.attack2_move() - defender2.attack2_move()
        return attack1_result, attack2_result

我收到以下错误:

AttributeError: 'Attack' 没有属性 'attack1_move'

最后,当我尝试这个时-

game.py

    xy = battle(Attack.punch(), Attack.kick())

combat_system.py

    def battle(attack1_move, attack2_move):
        attack1_result = attacker1.attack1_move() - defender1.attack1_move() 
        attack2_result = attacker2.attack2_move() - defender2.attack2_move()
        return attack1_result, attack2_result

我收到此错误:

TypeError:必须以 Attack 实例作为第一个参数调用未绑定的方法 punch()(什么都没有)

任何关于我做错了什么的帮助将不胜感激。提前致谢,如果我为了提供帮助而省略了任何必要的信息,请向 LMK 致谢。

标签: python

解决方案


这是您最有希望的尝试:

xy = battle(Attack.punch, Attack.kick)

为了使这项工作,你想要:

def battle(attack1_move, attack2_move):
    attack1_result = attack1_move(attacker1) - attack1_move(defender1) 
    attack2_result = attack2_move(attacker2) - attack2_move(defender2)
    return attack1_result, attack2_result

这里的关键点是,通常,my_obj.method(...)MyClass.method(my_obj, ...)


也可以使用

xy = battle('punch', 'kick')

作为:

def battle(attack1_move, attack2_move):
    attack1_result = getattr(attacker1, attack1_move)() - getattr(defender1, attack1_move)() 
    attack2_result = getattr(attacker2, attack2_move)() - getattr(defender2, attack2_move)()
    return attack1_result, attack2_result

推荐阅读